wpf XAML 绑定对依赖属性不起作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40184194/
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
XAML binding not working on dependency property?
提问by Oliver
I am trying (and failing) to do data binding on a dependency property in xaml. It works just fine when I use code behind, but not in xaml.
我正在尝试(但失败)对 xaml 中的依赖属性进行数据绑定。当我在后面使用代码时它工作得很好,但不是在 xaml 中。
The user control is simply a TextBlockthat bind to the dependency property:
用户控件只是一个TextBlock绑定到依赖属性的控件:
<UserControl x:Class="WpfTest.MyControl" [...]>
<TextBlock Text="{Binding Test}" />
</UserControl>
And the dependency property is a simple string:
依赖属性是一个简单的字符串:
public static readonly DependencyProperty TestProperty
= DependencyProperty.Register("Test", typeof(string), typeof(MyControl), new PropertyMetadata("DEFAULT"));
public string Test
{
get { return (string)GetValue(TestProperty); }
set { SetValue(TestProperty, value); }
}
I have a regular property with the usual implementation of INotifyPropertyChangedin the main window.
我有一个INotifyPropertyChanged在主窗口中通常实现的常规属性。
private string _myText = "default";
public string MyText
{
get { return _myText; }
set { _myText = value; NotifyPropertyChanged(); }
}
So far so good. If I bind this property to a TextBlockon the main window everything works just fine. The text update properly if the MyTextchanges and all is well in the world.
到现在为止还挺好。如果我将此属性绑定到TextBlock主窗口上的a一切正常。如果MyText更改并且一切正常,文本会正确更新。
<TextBlock Text="{Binding MyText}" />
However, if I do the same thing on my user control, nothing happens.
但是,如果我在我的用户控件上做同样的事情,什么也不会发生。
<local:MyControl x:Name="TheControl" Test="{Binding MyText}" />
And now the fun part is that if I do the very same binding in code behind it works!
现在有趣的部分是,如果我在它背后的代码中做同样的绑定工作!
TheControl.SetBinding(MyControl.TestProperty, new Binding
{
Source = DataContext,
Path = new PropertyPath("MyText"),
Mode = BindingMode.TwoWay
});
Why is it not working in xaml?
为什么它在 xaml 中不起作用?
回答by Clemens
The dependency property declaration must look like this:
依赖属性声明必须如下所示:
public static readonly DependencyProperty TestProperty =
DependencyProperty.Register(
"Test", typeof(string), typeof(MyControl), new PropertyMetadata("DEFAULT"));
public string Test
{
get { return (string)GetValue(TestProperty); }
set { SetValue(TestProperty, value); }
}
The binding in the UserControl's XAML must set the control instance as the source object, e.g. by setting the Bindings's RelativeSourceproperty:
UserControl 的 XAML 中的绑定必须将控件实例设置为源对象,例如通过设置 Bindings 的RelativeSource属性:
<UserControl x:Class="WpfTest.MyControl" ...>
<TextBlock Text="{Binding Test,
RelativeSource={RelativeSource AncestorType=UserControl}}"/>
</UserControl>
Also very important, neverset the DataContextof a UserControl in its constructor. I'm sure there is something like
同样非常重要的是,永远不要DataContext在其构造函数中设置UserControl 的 。我确定有类似的东西
DataContext = this;
Remove it, as it effectively prevents inheriting a DataContext from the UserConrol's parent.
删除它,因为它有效地阻止了从 UserConrol 的父级继承 DataContext。
By setting Source = DataContextin the Binding in code behind you are explicitly setting a binding source, while in
通过Source = DataContext在后面的代码中的 Binding 中设置,您显式地设置了一个绑定源,而在
<local:MyControl Test="{Binding MyText}" />
the binding source implicitly is the current DataContext. However, that DataContext has been set by the assignment in the UserControl's constructor to the UserControl itself, and is not the inherited DataContext (i.e. the view model instance) from the window.
绑定源隐式是当前的 DataContext。但是,该 DataContext 已通过 UserControl 构造函数中对 UserControl 本身的赋值设置,而不是从窗口继承的 DataContext(即视图模型实例)。

