2010-12-02 17 views
0

に伴う問題は、私は、トリガを有する2つのリストビューを有するよう選択された変更に濃い灰色の背景色と白の前景色。 問題は、最初のリストビューでアイテムを選択した後、2番目のリストビューでアイテムを選択すると、最初のリストビューのフォアグラウンドのアイテムが再び黒くならず、白く保たれるということです。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

ちょうど予感が...参照してください。 – Gishu

+0

@ Gishu - ありがとうございましたが、動作しませんでした – Erez

+0

@Erez - Ok。逆方向トリガを追加してください。つまり、IsSelected = "False"の場合、またはスタイル自体にデフォルトの状態を指定してください。彼らは動作しないときのトリガーは非常に迷惑することができます - 私は最近、1に直面 - このスルー読んで、あなたはどんなリードを取得するかどうかを確認http://geekswithblogs.net/thibbard/archive/2008/05/13/wpf---changing -the-of-a-togglebutton-when-checked.aspx – Gishu

答えて

0

あなたは、テンプレート内のトリガーの2間の干渉を取得しています。 ListView#1の値を最初に選択すると、最初のIsSelected Triggerがアクティブになります。これは、TemplateBindingから「Content」のTextBlock.Foreground値を、Whiteの固定値に置き換えます。

リストビュー#1は、第2のトリガ(IsSelectedとIsSelectionActiveためMultiTrigger)も活性化されたリストビュー#2にフォーカスを失いました。これにより、 "Bd"の背景が別の値(他のTriggerと同じ)に設定され、後でTriggersコレクションで宣言されるため、まだアクティブな前のTriggerが上書きされます。

同じことがフォアグラウンドセッターのために起こるはずですが、MultiTriggerに1人が親コントロールに代わりの「コンテンツ」でフォアグラウンドを設定しています。 "コンテンツ"はもはや親コントロールのForeground値を取得するためにTemplateBindingを使用しないため、最初のTriggerのWhite値は "Content"要素でアクティブのままです。 ResourceDictionaryで共有=「false」を `スタイル定義に(要素)あなたの問題を解決します:` Xを追加する場合

関連する問題