[英]UserControl with DataContext and DependencyProperty
I want to bind a DependencyProperty and DataContext to my UserControl. 我想将DependencyProperty和DataContext绑定到我的UserControl。 DataContext is working, but Setting the DependencyProperty has no effect.
DataContext工作正常,但是设置DependencyProperty无效。 This is my UserControl:
这是我的UserControl:
<MyProperty:MyPropertyControl DataContext="{Binding SelectedPerson}" IsEnabled="True"/>
And this is the CodeBehind of my UserControl: 这是我的UserControl的CodeBehind:
public static readonly DependencyProperty IsEnabledProperty =
DependencyProperty.Register(
"IsEnabled",
typeof(Boolean),
typeof(MyProperty:MyPropertyControl),
new FrameworkPropertyMetadata()
{
DefaultValue = false,
BindsTwoWayByDefault = false,
DefaultUpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged,
});
public MyPropertyControl()
{
InitializeComponent();
}
public Boolean IsEnabled
{
get { return (Boolean)GetValue(IsEnabledProperty); }
set
{
SetValue(IsEnabledProperty, value);
NotifyPropertyChanged("IsEnabled");
}
}
I can set the property IsEnabled to true or false, but it has no effect. 我可以将属性IsEnabled设置为true或false,但是它无效。
User Control Code: 用户控制代码:
<Button Content="Test" Width="100" Height="30" IsEnabled="{Binding IsEnabled}" />
You'll have to set the source object of the Binding to the UserControl, eg like this: 您必须将Binding的源对象设置为UserControl,例如:
<Button ... IsEnabled="{Binding IsEnabled,
RelativeSource={RelativeSource AncestorType=UserControl}}" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.