Home > Enterprise >  Converter inside DataTrigger
Converter inside DataTrigger

Time:11-21

I'm trying to display a different background color in wpf based on datatrigger and a converter.

<dxg:TableView.RowStyle>
    <Style TargetType="{x:Type dxg:RowControl}">
        <Setter Property="Height" Value="{StaticResource examRowHeight}" />
        <Style.Triggers>
            <DataTrigger Binding="{Binding Row.IsStatusRead, UpdateSourceTrigger=PropertyChanged}" Value="true">
                <Setter Property="Background" Value="Khaki" />
            </DataTrigger>
            <DataTrigger Binding="{Binding Row.IsRecordingFileOpened, UpdateSourceTrigger=PropertyChanged}" Value="true">
                <Setter Property="Background">
                    <Setter.Value>
                        <Binding Path="Row.ExamBlockType">
                            <Binding.Converter>
                                <valueConverters:ExamBlockTypeToBackgroundBrush />
                            </Binding.Converter>
                        </Binding>
                    </Setter.Value>
                </Setter>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</dxg:TableView.RowStyle>

Is this the right way? Converter seems not be fired.

Is there an alternative way to achieve the same behaviour?

Thanks in advance

Ric

CodePudding user response:

Maybe you need the converter on the datatrigger binding.

More like

        <DataTrigger Binding="{Binding Row.IsRecordingFileOpened
          , Converter={StaticResource YourConverter}}" Value="true">

Or maybe you should just use a multiconverter. I'm not totally clear what you're trying to do there.

If there is complicated logic involved then you could encapsulate that in the row viewmodel. Expose a string property used by a brushconverter. It is often simpler to write code in that case.

https://learn.microsoft.com/en-us/dotnet/api/system.windows.media.brushconverter?view=windowsdesktop-7.0

By exposing a string property for each colour you can avoid referencing UI orientated classes in your viewmodel.

CodePudding user response:

I found the following solution and I share it with all that will read this post

<dxg:TableView.RowStyle>
    <Style TargetType="{x:Type dxg:RowControl}">
        <Setter Property="Height" Value="{StaticResource examRowHeight}" />
        <Setter Property="Background">
            <Setter.Value>
                <MultiBinding Converter="{StaticResource ExamBlockTypeToBackgroundBrush}">
                    <Binding Path="Row.IsStatusRead" />
                    <Binding Path="Row.IsRecordingFileOpened" />
                    <Binding Path="Row.ExamBlockType" />
                </MultiBinding>
            </Setter.Value>
        </Setter>
    </Style>
</dxg:TableView.RowStyle> 

Where ExamBlockTypeToBackgroundBrush is a custom IMultiValueConverter where is implemented a switch case based on ExamBlockType enum that return

var converter = new System.Windows.Media.BrushConverter(); return (Brush)converter.ConvertFromString("#7500ABAE");
  • Related