M
M
Mikhail Usotsky2019-03-13 15:09:59
WPF
Mikhail Usotsky, 2019-03-13 15:09:59

How to make triggers work in ListViewItem?

There is a ListViewItem style:

<Style TargetType="ListViewItem">
    <Setter Property="SnapsToDevicePixels" Value="true" />
    <Setter Property="OverridesDefaultStyle" Value="true" />
    <Setter Property="local:Properties.IsStatus" Value="{Binding IsStatus, Mode=OneWay}" />
    <Setter Property="local:Properties.IsConnection" Value="{Binding IsConnection, Mode=OneWay}" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListViewItem}">
                <Border x:Name="Border" Padding="0">
                    <ContentPresenter
                        x:Name="Content"
                        Margin="0,0,0,0"
                        HorizontalAlignment="Stretch"
                        VerticalAlignment="Stretch" />
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>

    <Style.Triggers>

        <MultiTrigger>
            <MultiTrigger.Conditions>
                <Condition Property="local:Properties.IsStatus" Value="0" />
                <Condition Property="local:Properties.IsConnection" Value="True" />
            </MultiTrigger.Conditions>
            <MultiTrigger.Setters>
                <!--  Красный  -->
                <Setter Property="Background" Value="#FFFFCDD2" />
                <Setter Property="Foreground" Value="#FFB71C1C" />
                <Setter Property="BorderBrush" Value="#00D32F2F" />
            </MultiTrigger.Setters>
        </MultiTrigger>

        <MultiTrigger>
            <MultiTrigger.Conditions>
                <Condition Property="local:Properties.IsStatus" Value="1" />
                <Condition Property="local:Properties.IsConnection" Value="True" />
            </MultiTrigger.Conditions>
            <MultiTrigger.Setters>
                <!--  Оранжевый  -->
                <Setter Property="Background" Value="#FFFFE0B2" />
                <Setter Property="Foreground" Value="#FFE65100" />
                <Setter Property="BorderBrush" Value="#00F57C00" />
            </MultiTrigger.Setters>
        </MultiTrigger>

... (тут может несколько вариантов подобных установок)
...

        <Trigger Property="IsSelected" Value="True">
            <Setter Property="Background" Value="#FFFFE0B2" />
            <Setter Property="Foreground" Value="#FFE65100" />
        </Trigger>

        <MultiTrigger>
            <MultiTrigger.Conditions>
                <Condition Property="IsSelected" Value="False" />
                <Condition Property="local:Properties.IsConnection" Value="False" />
            </MultiTrigger.Conditions>
            <MultiTrigger.Setters>
                <Setter Property="Background" Value="#FFFFFFFF" />
                <Setter Property="Foreground" Value="#FF000000" />
                <Setter Property="BorderBrush" Value="#00000000" />
            </MultiTrigger.Setters>
        </MultiTrigger>

        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Background" Value="#FFE8EAF6" />
            <Setter Property="Foreground" Value="#FF1A237E" />
        </Trigger>
    </Style.Triggers>
</Style>

And there is a data template:
<DataTemplate DataType="{x:Type host_com:Host_Device_Items}">
    <DataTemplate.Resources>
        <local:IntToBooleanConverter x:Key="IntToBoolConverter" />
    </DataTemplate.Resources>
    <ListViewItem local:Properties.IsConnection="{Binding IsConnection}" local:Properties.IsStatus="{Binding IsStatus}">
        <Border >
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="50" />
                    <ColumnDefinition Width="8*" />
                    <ColumnDefinition Width="160" />
                    <ColumnDefinition Width="160" />
                    <ColumnDefinition Width="160" />
                    <ColumnDefinition Width="12*" />
                    <ColumnDefinition Width="4*" />
                </Grid.ColumnDefinitions>

                <Label
                    Grid.Column="0"
                    HorizontalAlignment="Right"
                    Content="{Binding Index}"
                    Foreground="{Binding Foreground}" />
                <Label
                    Grid.Column="1"
                    Margin="5,0,0,0"
                    Padding="0"
                    HorizontalAlignment="Left"
                    Content="{Binding Name}"
                    Foreground="{Binding Foreground}" />
                <RadioButton
                    Grid.Column="2"
                    local:Properties.IsStatusFalse="9"
                    local:Properties.IsStatusTrue="0"
                    Content="Выключен"
                    IsChecked="{Binding Selected, Converter={StaticResource IntToBoolConverter}, ConverterParameter=0}"
                    IsEnabled="{Binding IsEnabled, RelativeSource={RelativeSource Mode=Self}}" />
                <RadioButton
                    Grid.Column="3"
                    local:Properties.IsStatusFalse="9"
                    local:Properties.IsStatusTrue="3"
                    Content="1"
                    IsChecked="{Binding Selected, Converter={StaticResource IntToBoolConverter}, ConverterParameter=1}"
                    IsEnabled="{Binding IsEnabled, RelativeSource={RelativeSource Mode=Self}}" />
                <RadioButton
                    Grid.Column="4"
                    local:Properties.IsStatusFalse="9"
                    local:Properties.IsStatusTrue="3"
                    Content="2"
                    IsChecked="{Binding Selected, Converter={StaticResource IntToBoolConverter}, ConverterParameter=2}"
                    IsEnabled="{Binding IsEnabled, RelativeSource={RelativeSource Mode=Self}}" />
            </Grid>
        </Border>
    </ListViewItem>
</DataTemplate>

Previously, the data template was located in the style, in Template. But since I needed the second version of the list, the old version is no longer suitable. I had to put it in a separate template. But there was a problem: triggers stopped working. How to solve this problem?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mikhail Usotsky, 2019-03-14
@AquariusStar

Solved a problem. It turned out that in an attempt to solve the issue with data templates, I temporarily disabled the resource dictionary in App.xaml, which referred to this style. And then I forgot about him. That's why everything didn't work. Now it's fully working as it should.

C
cyber_roach, 2019-03-14
@cyber_roach

Because you created another ListViewItem in the DataTemplate (instead of the content of the first one)
In the general tree of objects, you got a ListViewItem nested in a ListViewItem, no wonder everything broke.
(DataTemplate is created for ContentPresenter in style)
https://docs.microsoft.com/en-us/dotnet/api/system...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question