访问同一控件xaml中的依赖项属性



在Wpf应用程序中,我有一个主窗口。我在同一个项目中添加了一个用户控件。在用户控件的.xaml.cs文件中,将添加一个Dependency属性(属性的"Value"名称)。

我想访问usercontrol.xaml中定义的依赖项属性。我知道在window.xaml或其他用户控件中创建控件实例时也可以这样做。

但是,是否可以访问.xaml中.xaml.cs中定义的依赖项属性?

基于Vivs答案更新的问题

好的。我错提了我的问题。尽管如此,就连我自己也不知道要访问。但我实际想问的问题是,是否可以从.xaml中设置依赖属性。类似于上面给出的例子,

<Grid CustomBackground ="{Binding Path= BackgroundColor}" />

<Grid CustomBackground ="Blue" />

是否可以在相同的.xaml中设置这样的自定义依赖项属性?

是的,这是可能的。

类似于:

.xaml

<UserControl x:Class="MvvmLight26.UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:local="clr-namespace:MvvmLight26"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d">
  <Grid Background="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:UserControl1}}, Path=CustomBackground}" />
</UserControl>

.xaml.cs

public partial class UserControl1 : UserControl {
  public static readonly DependencyProperty CustomBackgroundProperty =
    DependencyProperty.Register(
      "CustomBackground",
      typeof(Brush),
      typeof(UserControl1),
      new FrameworkPropertyMetadata(Brushes.Tomato));
  public UserControl1() {
    InitializeComponent();
  }
  public Brush CustomBackground {
    get {
      return (Brush)GetValue(CustomBackgroundProperty);
    }
    set {
      SetValue(CustomBackgroundProperty, value);
    }
  }
}

备用:

如果你说把UserControlDataContext作为自己:

public UserControl1() {
  InitializeComponent();
  DataContext = this;
}

然后在你的xaml中,你可以使用:

<Grid Background="{Binding Path=DataContext.CustomBackground}" />

更新:

对于新问题,

不太直接。

  • 如果自定义DP注册为附加属性,则可以"设置"该值(请记住,附加属性的行为和范围与普通DP不同。)
  • 如果你想把它保持为一个正常的DP,那么你可以把UserControl1从原始答案中保持原样(只是DP部分。你需要删除它的xaml部分,并在后面的代码中使它成为一个非分部类),然后把它派生成一个新的UserControl

类似于:

<local:UserControl1 x:Class="MvvmLight26.UserControl2"
                    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                    xmlns:local="clr-namespace:MvvmLight26"
                    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                    CustomBackground="Blue"
                    mc:Ignorable="d">
  <Grid />
</local:UserControl1>

您可以将UserControl1命名为类似"BaseUserControl"之类的名称,以表明它不是直接使用的。

  • 您也可以在相同的xaml中设置UserControl.Style的值

xaml:

<UserControl x:Class="MvvmLight26.UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:local="clr-namespace:MvvmLight26"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d">
  <UserControl.Style>
    <Style>
      <Setter Property="local:UserControl1.CustomBackground"
              Value="Blue" />
    </Style>
  </UserControl.Style>
  <Grid Background="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:UserControl1}}, Path=CustomBackground}" />
</UserControl>

最新更新