2010-12-02 5 views
0

두 개의 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

ResourceDictionary의 스타일 정의 (요소)에'x : Shared = "false"를 추가하면 문제가 해결되는지 확인하십시오. – Gishu

+0

@ Gishu - 감사하지만 작동하지 않았습니다. – Erez

+0

@Erez - Ok. 역방향 트리거를 추가하십시오. 즉, IsSelected = "False"이거나 스타일 자체에 기본 상태를 지정하십시오. 방아쇠는 작동하지 않을 때 매우 성가시다. 최근에 직면했다. 이것을 통해 읽고 리드가 있는지 확인한다. http://geekswithblogs.net/thibbard/archive/2008/05/13/wpf---changing -the-to-a-togglebutton-when-checked.aspx – Gishu

답변

0

당신은 당신의 템플릿에 트리거의 둘 사이의 간섭을 받고 있습니다. 첫 번째 IsSelected 트리거는 ListView # 1에서 값을 처음 선택할 때 활성화됩니다. 이 값은 TemplateBinding의 "Content"에있는 TextBlock.Foreground 값을 White의 고정 값으로 대체합니다.

ListView # 1이 ListView # 2에 포커스를 잃을 때 두 번째 트리거 (IsSelected 및 IsSelectionActive의 MultiTrigger)도 활성화됩니다. 이로 인해 "Bd"의 배경이 다른 값 (다른 트리거와 동일)으로 설정되고 나중에 트리거 컬렉션에서 선언 되었기 때문에 여전히 활성화되어있는 이전 트리거가 무시됩니다.

포 그라운드 설정 도구에서도 마찬가지지만 MultiTrigger에있는 포어 그라운드는 "컨텐트"대신 상위 컨트롤에서 포어 그라운드를 설정합니다. "Content"는 더 이상 TemplateBinding을 사용하여 상위 컨트롤의 Foreground 값을 가져 오지 않으므로 첫 번째 Trigger의 흰색 값은 "Content"요소에서 활성 상태로 유지됩니다.