简体   繁体   中英

Row DataTriggers on a WPF Grid

I have a ListBox with a style. This style layouts the ListBox with a 3 column layout based on a Grid . The Grid itself contains an Image and two TextBlock elements. Everything is working fine so far. Now I want to create a trigger that is changing the formatting and the content of the columns.

Currently I've managed to have Triggers on the TextBlocks within the grid, which is working fine. See the following code

        <Style TargetType="ListBox" x:Key="strechedItemStyle">
        <Setter Property="ItemTemplate">
        <Setter.Value>
            <DataTemplate>
                <Grid>
                            <Grid.Resources>

                            <Style x:Key="txtActive"  TargetType="TextBlock">
                                <Style.Triggers>
                                    <Trigger Property="Grid.Column" Value="0"></Trigger>
                                    <DataTrigger Binding="{Binding Path=IsActive}" Value="True">
                                        <Setter Property="FontWeight" Value="Bold"/>
                                        <Setter Property="Text" Value="(Active)"/>
                                    </DataTrigger>

                                    <DataTrigger Binding="{Binding Path=IsActive}" Value="False">
                                        <Setter Property="FontWeight" Value="Normal"/>
                                        <Setter Property="Text" Value=""/>
                                    </DataTrigger>

                                </Style.Triggers>
                            </Style>
                        </Grid.Resources>
                            <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>
                        <Image Source="../Images/copy.png" Height="16" Width="16"  HorizontalAlignment="Left" Grid.Column="0" Margin="2 0 5 0" />
                        <TextBlock Style="{StaticResource txtActive}" Text="{Binding Path=Name}" HorizontalAlignment="Left" Name="EnvName"  Grid.Column="1"/>
                        <TextBlock Style="{StaticResource txtActive}"  Name="Active" HorizontalAlignment="Right" Margin="0 0 5 0" Grid.Column="2">
                        </TextBlock>
                    </Grid>
            </DataTemplate>
        </Setter.Value>
    </Setter>
</Style>

But I don't really like the approach of having to style each column that way, especially because I am altering the content of the column rather than the column itself, or is this the wrong thinking? What if I don't know the content of a column?

I would rather like to have one trigger on the grid that is ie changing the Font-Weight for the whole row.

Alternatively: Is there a way how to have a trigger on individual Grid columns rather than their contents?

If you want to set the fontweight with one trigger on the grid style you just need to do this:

<Grid.Style>
    <Style TargetType="Grid">
        <Style.Triggers>
            <DataTrigger Binding="{Binding Path=IsActive}" Value="True">
                <Setter Property="TextBlock.FontWeight" Value="Bold"/>
                <Setter Property="TextBlock.Text" Value="(Active)"/>
        </DataTrigger>
        </Style.Triggers>
    </Style>
</Grid.Style>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM