wpf - 在样式 wpf 的 setter 中使用转换器

标签 wpf

我有一个转换器可以将 Double.Nan 转换为 null。 我需要在资源词典中引用它。 我将在这里包含我的代码

  <Style x:Key="LabelStyle" TargetType="{x:Type Label}" >
  <Setter Property="Content">
            <Setter.Value>
                <Binding Path="Content" RelativeSource="{RelativeSource Self}">
                    <Binding.Converter>
                        <local:NanToNullConverter/>
                    </Binding.Converter>
                </Binding>
            </Setter.Value>
        </Setter>
    </Style>

这个转换器被触发。但是 UI 中的值没有更新。 绑定(bind)是这样完成的

<Label Style="{DynamicResource LabelStyle}" Content="{Binding Filters_TanksModelObject.RunDown_HeaderPressureDischargeStart ,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" ContentStringFormat="#.##" ></Label>

最佳答案

本地属性优于样式属性,因此您的 <Setter Property="Content">被忽略,只有 Content="{Binding Filters_TanksModelObject.RunDown_HeaderPressureDischargeStart ,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"被使用。

相反,使用

<Style x:Key="LabelStyle" TargetType="{x:Type Label}" >
    <Setter Property="Content">
        <Setter.Value>
            <Binding Path="Filters_TanksModelObject.RunDown_HeaderPressureDischargeStart" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged">
                <Binding.Converter>
                    <local:NanToNullConverter/>
                </Binding.Converter>
            </Binding>
        </Setter.Value>
    </Setter>
</Style>

并删除直接赋值。或者直接使用此绑定(bind)而不依赖样式。

关于wpf - 在样式 wpf 的 setter 中使用转换器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42897533/

相关文章:

WPF:如何将重点放在数据网格的特定行上?

wpf - ViewModel中的INotifyPropertyChanged与DependencyProperty

wpf - 使用 LoadComponent 加载外部 XAML 文件

wpf - 在wpf中检查打印机的纸张是空的

wpf - WPF 中有 OpenForms 属性吗?

c# - 删除了我的 App.xml 和 App.xml.cs 文件

c# - 如何在 Windows XP 上运行 nAudio?

WPF 窗口大小

WPF 绑定(bind)到局部变量

c# - 为什么 ListBox 的 DesiredSize.Width 始终为零?