WPF:将 DataTrigger 与 IsMouseOver 相结合
WPF: Combine DataTrigger with IsMouseOver
这是我的绑定数据:
List<MyData> files;
public clas MyData
{
public string Name { get; set; } // Column
public bool IsOK { get; set; } // Not a Column
public string Format { get; set; } // Column
}
我的ListView
:
<ListView
Grid.Row="0"
Name="lvFiles"
ItemsSource="{Binding wiresharkFiles}">
<ListView.ItemContainerStyle>
<DataTrigger Binding="{Binding IsOK}" Value="True">
<Setter Property="Foreground" Value="Red" />
</DataTrigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsMouseOver" Value="True"/>
</MultiTrigger.Conditions>
<Setter Property="Foreground" Value="White"></Setter>
<Setter Property="Background" Value="Green"/>
</MultiTrigger>
</ListView.ItemContainerStyle>
我有几个 ListViewColumns
因为我只想更改名称列的颜色 我将其添加到我的其他列(格式列)GridViewColumn
:
<GridViewColumn Width="115" Header="Format">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock
x:Name="textBlock"
Text="{Binding FileFormat}"
Margin="0,0,0,0"/>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding IsOK}" Value="True">
<Setter TargetName="textBlock" Property="Foreground" Value="Silver"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
这不会将此 Column
(格式列)标记为红色,但如果 IsMouseIsOver
是 true
,则此 column
仍在 Silver
而不是变成 White
就像在我的 trigger
.
中定义
所以我将 GridViewColumn
更改为:
<GridViewColumn Width="115" Header="Format">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock
x:Name="textBlock"
Text="{Binding FileFormat}"
Margin="0,0,0,0">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Foreground" Value="Gray"/>
</Trigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding BadCheckSumExist}" Value="True">
<Setter TargetName="textBlock" Property="Foreground" Value="Silver"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
但这仍然没有按预期工作。
将触发器保持在 ItemContainerStyle
。只需将 Foreground
绑定添加到 TextBlock
,这将从 ListViewItem
的 Foreground
.
获取前景画笔
<TextBlock x:Name="textBlock"
Text="{Binding FileFormat}"
Foreground="{Binding Path=Foreground, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListViewItem}}}"
Margin="0,0,0,0"/>
这是我的绑定数据:
List<MyData> files;
public clas MyData
{
public string Name { get; set; } // Column
public bool IsOK { get; set; } // Not a Column
public string Format { get; set; } // Column
}
我的ListView
:
<ListView
Grid.Row="0"
Name="lvFiles"
ItemsSource="{Binding wiresharkFiles}">
<ListView.ItemContainerStyle>
<DataTrigger Binding="{Binding IsOK}" Value="True">
<Setter Property="Foreground" Value="Red" />
</DataTrigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsMouseOver" Value="True"/>
</MultiTrigger.Conditions>
<Setter Property="Foreground" Value="White"></Setter>
<Setter Property="Background" Value="Green"/>
</MultiTrigger>
</ListView.ItemContainerStyle>
我有几个 ListViewColumns
因为我只想更改名称列的颜色 我将其添加到我的其他列(格式列)GridViewColumn
:
<GridViewColumn Width="115" Header="Format">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock
x:Name="textBlock"
Text="{Binding FileFormat}"
Margin="0,0,0,0"/>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding IsOK}" Value="True">
<Setter TargetName="textBlock" Property="Foreground" Value="Silver"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
这不会将此 Column
(格式列)标记为红色,但如果 IsMouseIsOver
是 true
,则此 column
仍在 Silver
而不是变成 White
就像在我的 trigger
.
所以我将 GridViewColumn
更改为:
<GridViewColumn Width="115" Header="Format">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock
x:Name="textBlock"
Text="{Binding FileFormat}"
Margin="0,0,0,0">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Foreground" Value="Gray"/>
</Trigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding BadCheckSumExist}" Value="True">
<Setter TargetName="textBlock" Property="Foreground" Value="Silver"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
但这仍然没有按预期工作。
将触发器保持在 ItemContainerStyle
。只需将 Foreground
绑定添加到 TextBlock
,这将从 ListViewItem
的 Foreground
.
<TextBlock x:Name="textBlock"
Text="{Binding FileFormat}"
Foreground="{Binding Path=Foreground, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListViewItem}}}"
Margin="0,0,0,0"/>