2010-12-02 34 views
0

我有两个带有触发器的ListViews,它们会将背景颜色更改为深灰色,将前景色更改为白色。 问题是,当我在第一个列表视图中选择一个项目,然后在第二个列表视图中选择一个项目时,第一个listview前景中的项目不再变黑,并且它保持白色。WPF - 带触发器的问题

alt text

的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> 
+0

只是一个预感......看看是否将`x:Shared =“false”`添加到ResourceDictionary中的样式定义(元素)可以解决您的问题。 – Gishu 2010-12-02 08:47:05

回答

0

你得到你的模板两个触发器之间的干扰。当您第一次选择ListView#1中的值时,第一个IsSelected触发器变为活动状态。这将覆盖TemplateBinding中“内容”的TextBlock.Foreground值为固定值White。

当ListView#1失去焦点到ListView#2时,第二个触发器(用于IsSelected和IsSelectionActive的MultiTrigger)也被激活。这导致“Bd”的背景设置为不同的值(与其他触发器相同),并且因为它稍后在Triggers集合中声明,所以会覆盖仍然处于活动状态的前一个触发器。

前台设置人员应该会发生同样的情况,但MultiTrigger中的设置是在父级控件上设置Foreground而不是“内容”。因为“内容”不再使用TemplateBinding来拉入父控件的前景值,所以第一个触发器的白色值在“Content”元素上保持活动状态。