0

The following xaml code is the four resources I am using. I want to merge two style resources and two data templates into a style resource and a data template, respectively. It would be very happy if all of them could be merged into one resource.

Thanks for help!

        <Style x:Key="ServiceStatusImageStyle" TargetType="{x:Type Image}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding ServiceStatus}" Value="true">
                    <Setter Property="Source" Value="/Resources/Images/StartedService.png"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding ServiceStatus}" Value="false">
                    <Setter Property="Source" Value="/Resources/Images/StopedService.png"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>

        <Style x:Key="DrawModeImageStyle" TargetType="{x:Type Image}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding bScrollViewMode}" Value="true">
                    <Setter Property="Source" Value="/Resources/Images/scrollview24.png"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding bScrollViewMode}" Value="false">
                    <Setter Property="Source" Value="/Resources/Images/zoomview24.png"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>

        <DataTemplate x:Key="ServiceStatusDataTemplate" DataType="{x:Type local:MainWindowViewModel}">
            <Grid Width="220" Height="60">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="35"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <Image Grid.Column="0" Width="30" Height="30" Margin="5,0,0,0" Style="{StaticResource ServiceStatusImageStyle}"/>
                <TextBlock Grid.Column="1" FontSize="25" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="5,0,0,0" 
                           Foreground="White" Text="{Binding ServiceStatusString}">
                </TextBlock>
            </Grid>
        </DataTemplate>

        <DataTemplate x:Key="DrawModeDataTemplate" DataType="{x:Type local:MainWindowViewModel}">
            <Grid Width="220" Height="60">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="35"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <Image Grid.Column="0" Width="30" Height="30" Margin="5,0,0,0" Style="{StaticResource DrawModeImageStyle}"/>
                <TextBlock Grid.Column="1" FontSize="25" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="5,0,0,0" 
                           Foreground="White" Text="{Binding ViewModeName}">
                </TextBlock>
            </Grid>
        </DataTemplate>
atoi
  • 1
  • Not sure where the problem is. You can of course have a single Image Style with DataTriggers for more than one binding source property. As a note, you don't need two triggers for the same Binding. Just put the default value in a regular Style Setter. For the DataTemplates, you need of course two, because the TextBlocks bind to different view model properties. – Clemens Feb 09 '19 at 12:57
  • There are some different techniques to define an indirect binding, described - for example - [here](https://stackoverflow.com/questions/17598200/datatrigger-binding-in-wpf-style/17600339). The simplest (but maybe not the best) way is to use the tag property. However, you are not only specifying different binding variables, but also two different images in each style. I think you could handle this with 3 attached properties for the binding variable and the 2 different images. However, in this case, I don't think it has any benefit. – Phil Jollans Mar 01 '19 at 22:10

0 Answers0