在WPF样式的setter中使用转换器

3

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

这个转换器已被触发。但是界面上的数值没有更新。 绑定方式如下:
<Label Style="{DynamicResource LabelStyle}" Content="{Binding Filters_TanksModelObject.RunDown_HeaderPressureDischargeStart ,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" ContentStringFormat="#.##" ></Label>
4个回答

1
本地属性优先于样式属性,因此您的<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>

并移除直接赋值。或者直接使用此绑定,不要依赖于样式。

@user1665130 这是一个不错的解决方案,可以帮助您解决问题。 - WPFUser

0
尝试这个解决方案:
1. 将您的转换器添加为静态资源。
<UserControl.Resources>
    <local:NanToNullConverter x:Key="nanToNullCnvrt"/>
</UserControl.Resources>

2 将转换器直接添加到 Content 属性中

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

我们可以直接添加转换器,通过StaticResource添加并不是唯一的方式。为什么我们使用StaticResource是因为我们可以在多个地方使用它们。 - WPFUser
1
@WPFUser 资源转换器的另一个原因是,它可以被放置在绑定字符串中,而不必使转换器本身成为“MarkupExtension”。 - grek40

0

转换器应该应用于绑定本身,因此将其放在Style中并没有太多意义,我很抱歉。

@grek40的解决方案在Style中硬编码了绑定路径,这使得Style几乎无用,如果您的意图是创建一个通用的Label样式,以适用于所有绑定。

不要试图在Style中包含转换器,并将其应用于每个绑定:

<Application.Resources>
    <local:NanToNullConverter x:Key="NanToNullConverter" />
</Application.Resources>


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

另一个选择是创建一个自定义的Label控件,处理从Double.Nannull的转换,而不使用转换器。

-1
 <Binding Path="Content" RelativeSource="{RelativeSource Self}">

在这里使用RelativeSource,会将Binding作为源,并查找Binding中的路径“Content”,这将以绑定错误结束。 如果您确定DataContext的类型,可以访问正确的路径,而无需指定源。
或者,
<Binding Path="Content" RelativeSource={RelativeSource AncestorType={x:Type Label}}>

如果它在模板内部,
 <Binding Path="Content" RelativeSource="{RelativeSource TemplatedParent}">

我不是那个给你点踩的人,但是没有发现 ControlTemplate 的迹象,所以使用 TemplatedParent 相对源是错误的。 - grek40

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接