c# - 从 xaml 中清除 dependencyProperty 值

标签 c# wpf xaml binding .net-4.0

我有一个控件,它有一个属性的默认值。当控件首次设置其 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 控件的默认值,并允许用户取消设置或将其更改为其他内容。

最佳答案

关于c# - 从 xaml 中清除 dependencyProperty 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7969315/

相关文章:

wpf - 当 WPF DataGridCell 禁用时如何检测鼠标双击事件?

c# - 将 XAML 中 WPF 窗口的启动位置更改为右下角

.net - “Clickable”文字区 block ?

c# - 为 ASP.NET 网站创建 web.sitemap 文件

c# - 为什么 Bitmap.LockBits 在虚拟机上失败

c# - 鼠标事件被后面的元素搞砸了?

c# - 在 ItemsControl DataTemplate 中设置 Canvas 属性

c# - 从基类的虚方法访问时TestContext为null

c# - 如何在 asp .net 中的 gridview 中的单个列中的 HeaderTemplate 中设置标签的对齐方式?

c# - Xamarin.Forms.Xaml.XamlParseException : Cannot assign property "IsVisable" : Property does not exists