Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using convereter in a setter of style wpf

Tags:

wpf

I have a converter to convert Double.Nan to null. I need to refer this in resourcedictionary. I will include my code here

  <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>

This convereter is triggered. But value is not updating in the UI. Binding is done like this

<Label Style="{DynamicResource LabelStyle}" Content="{Binding Filters_TanksModelObject.RunDown_HeaderPressureDischargeStart ,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" ContentStringFormat="#.##" ></Label>
like image 592
user1665130 Avatar asked Jan 30 '26 23:01

user1665130


1 Answers

Local properties are preferred over style properties, so your <Setter Property="Content"> is ignored and only the Content="{Binding Filters_TanksModelObject.RunDown_HeaderPressureDischargeStart ,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" is used.

Instead, use

<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>

and remove the direct assignment. Or use this binding directly and don't rely on the style.

like image 193
grek40 Avatar answered Feb 03 '26 09:02

grek40