我有兩個帶有觸發器的ListViews,它們會將背景顏色更改爲深灰色,將前景色更改爲白色。 問題是,當我在第一個列表視圖中選擇一個項目,然後在第二個列表視圖中選擇一個項目時,第一個listview前景中的項目不再變黑,並且它保持白色。WPF - 帶觸發器的問題
的XAML:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="190*" />
<RowDefinition Height="121*" />
</Grid.RowDefinitions>
<Grid.Resources>
<ResourceDictionary>
<Style x:Key="@ListViewItemStyle" TargetType="{x:Type ListViewItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType='{x:Type ListViewItem}'>
<Grid SnapsToDevicePixels="True" Margin="0">
<Border x:Name="Bd" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}" />
<GridViewRowPresenter x:Name="Content" TextBlock.Foreground="{TemplateBinding Foreground}"
Content="{TemplateBinding Content}" Columns="{TemplateBinding GridView.ColumnCollection}" />
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="true">
<Setter Property="TextElement.Foreground" Value="White" TargetName="Content" />
<Setter Property="Background" Value="DarkGray" TargetName="Bd"/>
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsSelected" Value="true" />
<Condition Property="Selector.IsSelectionActive" Value="false" />
</MultiTrigger.Conditions>
<Setter Property="Background" TargetName="Bd"
Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" />
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" />
</MultiTrigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<DataTemplate x:Key="@TextCellTemplate">
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
<DataTemplate x:Key="@TrubleCellTemplate">
<Rectangle Width="20" Height="20" Fill="Black"></Rectangle>
</DataTemplate>
</ResourceDictionary>
</Grid.Resources>
<ListView ItemsSource="{Binding Persons}" Style="{DynamicResource @ListView}" ItemContainerStyle="{DynamicResource @ListViewItemStyle}">
<ListView.View>
<GridView>
<GridViewColumn Width="40" CellTemplate="{DynamicResource @TextCellTemplate}" />
<GridViewColumn Width="131" CellTemplate="{DynamicResource @TrubleCellTemplate}" />
</GridView>
</ListView.View>
</ListView>
<ListView ItemsSource="{Binding Persons}" Style="{DynamicResource @ListView}" ItemContainerStyle="{DynamicResource @ListViewItemStyle}" Grid.Row="1">
<ListView.View>
<GridView>
<GridViewColumn Width="40" CellTemplate="{DynamicResource @TextCellTemplate}" />
<GridViewColumn Width="131" CellTemplate="{DynamicResource @TrubleCellTemplate}" />
</GridView>
</ListView.View>
</ListView>
</Grid>
只是一個預感......看看是否將`x:Shared =「false」`添加到ResourceDictionary中的樣式定義(元素)可以解決您的問題。 – Gishu 2010-12-02 08:47:05