我有一个转换器可以将 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/