我有一个控件,它有一个属性的默认值。当控件首次设置其 dataContext 时,它会自动分配此属性。
现在在 xaml 中,我希望可以取消设置此属性。我已经尝试将它设置为 x:Null 只是空字符串,但随后出现错误,因为没有该属性的转换器。在我希望禁用该功能的极少数情况下,如何简单地从 xaml 中取消分配该属性?
原来设置的代码:
void OmniBox_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
{
if( e.NewValue is BindingObjectBaseExtended )
{
BindingObjectBaseExtended value = (BindingObjectBaseExtended)e.NewValue;
this.SetBinding(OmniBox.ContextValidationMessagesProperty, new Binding() { Source = value, Path = new PropertyPath("ValidationMessages") });
}
}
我想在其中取消设置属性的 xaml。
<Style TargetType="ui:OmniBox">
<Setter Property="ContextValidationMessages" Value="" />
</Style>
请注意,如果我没有在数据上下文更改时自动设置绑定(bind),则默认情况下没有验证消息,我必须在 xaml 中执行以下操作来设置它们:
<Style TargetType="ui:OmniBox">
<Setter Property="ContextValidationMessages" Value="ValidationMessages" />
</Style>
我想做的是将上述绑定(bind)设置为我的自定义 OmniBox 控件的默认值,并允许用户取消设置或将其更改为其他内容。
最佳答案
DependencyProperty.UnsetValue 不能在 XAML 中使用。
http://msdn.microsoft.com/en-us/library/system.windows.dependencyproperty.unsetvalue(v=vs.90).ASPX
关于c# - 从 xaml 中清除 dependencyProperty 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7969315/