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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17468515/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:08:50  来源:igfitidea点击:

Acessing dependency property in the same control xaml

wpfc#-4.0wpf-controlsc#-3.0

提问by madhu sudhan

In a Wpf Application i have a main window. I have added a user control to the same project. In the user control's .xaml.cs file a Dependency property ( "Value" name of the property ) is added.

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

I would like to access the defined dependency property in the usercontrol.xaml. I know i can do the same while creating the control instance either in window.xaml or some other user control.

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

But is it possible to access the dependency property defined in .xaml.cs in .xaml?

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

Question updated based on Vivs answer

问题根据 Vivs 答案更新

Ok. I mentioned my question wrongly. Nevertheless even i was not aware of accessing. But my actual intended question is it possible to set the dependency property from .xaml. some thing like from the example given above,

好的。我错误地提到了我的问题。尽管如此,即使我不知道访问。但我的实际预期问题是可以从 .xaml 设置依赖属性。像上面给出的例子中的一些东西,

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

Or

或者

<Grid CustomBackground ="Blue" />

Is it possible to set the custom dependency properties like this in the same .xaml?

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

回答by Viv

Yes it is possible.

对的,这是可能的。

something like:

就像是:

.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>

and .xaml.cs:

.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);
    }
  }
}

Alternate:

备用:

If you say have the DataContextof the UserControlas itself like:

如果你说 has DataContextthe UserControlas 本身就像:

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

then in your xaml you could just go with:

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

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

Update:

更新:

For the new question,

对于新问题,

Not quite directly.

不是很直接。

  • You can "set" the value if the custom DP is registered as an attached property(Do remember an attached property is not the same as a normal DP in it's behavior and scope.)
  • If you want to keep it as a normal DP, then you can keep UserControl1from the original answer same as it is(just the DP part. You need to remove the xaml part of it and make it a non-partial class in the code-behind) and then derive it to a new UserControl.
  • 如果自定义 DP 注册为附加属性,您可以“设置”该值(请记住,附加属性的行为和范围与普通 DP 不同。)
  • 如果您想将其保留为普通 DP,那么您可以保持UserControl1原始答案不变(只是 DP 部分。您需要删除它的 xaml 部分并使其成为代码中的非部分类 -后面),然后将其派生为一个新的UserControl.

something like:

就像是:

<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>

You can ofc name UserControl1as something like "BaseUserControl" or so to make it obvious that it's not intended for direct usage.

您可以将UserControl1诸如“BaseUserControl”之类的名称命名为“BaseUserControl”之类的名称,以表明它不适合直接使用。

  • You can set the value from the UserControl.Stylein the same xaml as well.
  • 您也可以UserControl.Style在同一个 xaml 中设置值。

xaml:

xml:

<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>