ListViewItem前景色基于Binding触发器仅适用于特定列

时间:2017-06-27 16:59:19

标签: wpf listview

所以这是我的班级:

public class MyData
{
   public string Name;
   public bool IsOK;
}

这是我的收藏:

List<MyData> files;

包含所有y触发器的列表视图:

<ListView
    Name="lvFiles"
    Background="Transparent"
    BorderBrush="Gray"
    BorderThickness="0,0,0,0"
    ItemsSource="{Binding files}"

    <ListView.ItemContainerStyle>
        <Style TargetType="{x:Type ListViewItem}">
            <Setter Property="Foreground" Value="Silver"/>
            <Setter Property="Background" Value="Transparent"/>
            <Setter Property="BorderBrush" Value="Transparent"/>
            <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ListViewItem}">
                        <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" CornerRadius="2" SnapsToDevicePixels="true">
                            <Border x:Name="InnerBorder" BorderThickness="1" CornerRadius="1">
                                <Grid>
                                    <Grid.RowDefinitions>
                                        <RowDefinition MaxHeight="11"/>
                                        <RowDefinition/>
                                    </Grid.RowDefinitions>
                                    <!--<Rectangle x:Name="UpperHighlight" Fill="#75FFFFFF" Visibility="Collapsed"/>-->
                                    <GridViewRowPresenter Grid.RowSpan="2" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                                </Grid>
                            </Border>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <Trigger Property="IsEnabled" Value="False">
                    <Setter Property="TextElement.Foreground" Value="Gainsboro"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </ListView.ItemContainerStyle>
</ListView>

所以我想添加另一个trigger

如果我的媒体资源IsOKfalse,我希望我的Foreground颜色为Red 所以我补充说:

<DataTrigger Binding="{Binding IsOK}" Value="True">
    <Setter Property="Foreground" Value="Salmon" />
</DataTrigger>

所以这可行查找,但这改变了我的所有列Foreground颜色,我只想更改此GridViewColumn

<GridViewColumn Width="380" Header="File name" >
    <GridViewColumn.CellTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <Image 
                    Width="12"
                    Height="12"
                    x:Name="myImage"
                    Margin="0,0,0,0"/>
                <Image
                    Width="18"
                    Height="18"
                    Source="pack://application:,,,/Resources/pic.ico"
                    Margin="5,0,0,0"/>
                <TextBlock 
                    Text="{Binding File}" ToolTip="{Binding Name}"
                    Margin="5,0,0,0"/>
            </StackPanel>
            <DataTemplate.Triggers>
                <DataTrigger Binding="{Binding IsOK}" Value="False">
                    <Setter TargetName="myImage" Property="Source" Value="pack://application:,,,/Resources/pic2.png"/>
                    <Setter TargetName="myImage" Property="ToolTip" Value="OK"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding IsOK}" Value="True">
                    <Setter TargetName="myImage" Property="Source" Value="pack://application:,,,/Resources/rfew.png"/>
                    <Setter TargetName="myImage" Property="ToolTip" Value="false"/>
                </DataTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>
    </GridViewColumn.CellTemplate>
</GridViewColumn>

1 个答案:

答案 0 :(得分:0)

尝试使用

<ControlTemplate.Triggers> 

而不是:

<Style.Triggers>

我会举例说明:

<ControlTemplate.Triggers>
       <Trigger Property="IsSelected" Value="True">
              <Setter Property="Background" Value="#2D6292"/>
              <Setter Property="Foreground" Value="White"/>
       </Trigger>
</ControlTemplate.Triggers>