2013-04-20 6 views
1

다음과 같은 문제점이 있습니다. WPF 앱에 두 개의 treeview 컨트롤이 있고 첫 번째 트리의 항목 중 하나를 클릭 한 다음 두 번째 트리의 항목 중 하나를 클릭하면 첫 번째 트리의 항목이 해당 항목을 변경합니다. 색깔. multitrigger에서 배경색을 변경하려고 시도했지만 효과가없는 템플릿 속성은 배경색을 변경하려고했습니다.treeviewitem 배경색 변경을 비활성화하는 방법은 무엇입니까?

이 동작을 어떻게 비활성화 할 수 있습니까? 여기 내 샘플 창 코드

<Window x:Class="StackOverflowTests.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" Height="350" Width="525"> 

<Window.Resources> 
    <SolidColorBrush x:Key="ListBorder" Color="#828790"/> 
    <Style x:Key="TreeViewStyle1" TargetType="{x:Type TreeView}"> 
     <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/> 
     <Setter Property="BorderBrush" Value="{StaticResource ListBorder}"/> 
     <Setter Property="BorderThickness" Value="1"/> 
     <Setter Property="Padding" Value="1"/> 
     <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/> 
     <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/> 
     <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/> 
     <Setter Property="ScrollViewer.PanningMode" Value="Both"/> 
     <Setter Property="Stylus.IsFlicksEnabled" Value="False"/> 
     <Setter Property="VerticalContentAlignment" Value="Center"/> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type TreeView}"> 
        <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" SnapsToDevicePixels="true"> 
         <ScrollViewer x:Name="_tv_scrollviewer_" Background="{TemplateBinding Background}" CanContentScroll="false" Focusable="false" HorizontalScrollBarVisibility="{TemplateBinding ScrollViewer.HorizontalScrollBarVisibility}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalScrollBarVisibility="{TemplateBinding ScrollViewer.VerticalScrollBarVisibility}"> 
          <ItemsPresenter/> 
         </ScrollViewer> 
        </Border> 
        <ControlTemplate.Triggers> 
         <Trigger Property="IsEnabled" Value="false"> 
          <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/> 
         </Trigger> 
         <Trigger Property="VirtualizingPanel.IsVirtualizing" Value="true"> 
          <Setter Property="CanContentScroll" TargetName="_tv_scrollviewer_" Value="true"/> 
         </Trigger> 
        </ControlTemplate.Triggers> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
     <Style.Triggers> 
      <Trigger Property="VirtualizingPanel.IsVirtualizing" Value="true"> 
       <Setter Property="ItemsPanel"> 
        <Setter.Value> 
         <ItemsPanelTemplate> 
          <VirtualizingStackPanel/> 
         </ItemsPanelTemplate> 
        </Setter.Value> 
       </Setter> 
      </Trigger> 
     </Style.Triggers> 
    </Style> 
    <Style x:Key="TreeViewItemFocusVisual"> 
     <Setter Property="Control.Template"> 
      <Setter.Value> 
       <ControlTemplate> 
        <Rectangle/> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
    <SolidColorBrush x:Key="TreeViewItem.TreeArrow.Static.Checked.Fill" Color="#FF595959"/> 
    <SolidColorBrush x:Key="TreeViewItem.TreeArrow.Static.Checked.Stroke" Color="#FF262626"/> 
    <SolidColorBrush x:Key="TreeViewItem.TreeArrow.MouseOver.Stroke" Color="#FF1BBBFA"/> 
    <SolidColorBrush x:Key="TreeViewItem.TreeArrow.MouseOver.Fill" Color="Transparent"/> 
    <SolidColorBrush x:Key="TreeViewItem.TreeArrow.MouseOver.Checked.Stroke" Color="#FF262626"/> 
    <SolidColorBrush x:Key="TreeViewItem.TreeArrow.MouseOver.Checked.Fill" Color="#FF595959"/> 
    <PathGeometry x:Key="TreeArrow" Figures="M0,0 L0,6 L6,0 z"/> 
    <SolidColorBrush x:Key="TreeViewItem.TreeArrow.Static.Fill" Color="Transparent"/> 
    <SolidColorBrush x:Key="TreeViewItem.TreeArrow.Static.Stroke" Color="#FF989898"/> 
    <Style x:Key="ExpandCollapseToggleStyle" TargetType="{x:Type ToggleButton}"> 
     <Setter Property="Focusable" Value="False"/> 
     <Setter Property="Width" Value="16"/> 
     <Setter Property="Height" Value="16"/> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type ToggleButton}"> 
        <Border Background="Transparent" Height="16" Padding="5,5,5,5" Width="16"> 
         <Path x:Name="ExpandPath" Data="{StaticResource TreeArrow}" Fill="{StaticResource TreeViewItem.TreeArrow.Static.Fill}" Stroke="{StaticResource TreeViewItem.TreeArrow.Static.Stroke}"> 
          <Path.RenderTransform> 
           <RotateTransform Angle="135" CenterY="3" CenterX="3"/> 
          </Path.RenderTransform> 
         </Path> 
        </Border> 
        <ControlTemplate.Triggers> 
         <Trigger Property="IsChecked" Value="True"> 
          <Setter Property="RenderTransform" TargetName="ExpandPath"> 
           <Setter.Value> 
            <RotateTransform Angle="180" CenterY="3" CenterX="3"/> 
           </Setter.Value> 
          </Setter> 
          <Setter Property="Fill" TargetName="ExpandPath" Value="{StaticResource TreeViewItem.TreeArrow.Static.Checked.Fill}"/> 
          <Setter Property="Stroke" TargetName="ExpandPath" Value="{StaticResource TreeViewItem.TreeArrow.Static.Checked.Stroke}"/> 
         </Trigger> 
         <Trigger Property="IsMouseOver" Value="True"> 
          <Setter Property="Stroke" TargetName="ExpandPath" Value="{StaticResource TreeViewItem.TreeArrow.MouseOver.Stroke}"/> 
          <Setter Property="Fill" TargetName="ExpandPath" Value="{StaticResource TreeViewItem.TreeArrow.MouseOver.Fill}"/> 
         </Trigger> 
         <MultiTrigger> 
          <MultiTrigger.Conditions> 
           <Condition Property="IsMouseOver" Value="True"/> 
           <Condition Property="IsChecked" Value="True"/> 
          </MultiTrigger.Conditions> 
          <Setter Property="Stroke" TargetName="ExpandPath" Value="{StaticResource TreeViewItem.TreeArrow.MouseOver.Checked.Stroke}"/> 
          <Setter Property="Fill" TargetName="ExpandPath" Value="{StaticResource TreeViewItem.TreeArrow.MouseOver.Checked.Fill}"/> 
         </MultiTrigger> 
        </ControlTemplate.Triggers> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
    <Style x:Key="TreeViewItemStyle1" TargetType="{x:Type TreeViewItem}"> 
     <Setter Property="Background" Value="Transparent"/> 
     <Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/> 
     <Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/> 
     <Setter Property="Padding" Value="1,0,0,0"/> 
     <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/> 
     <Setter Property="FocusVisualStyle" Value="{StaticResource TreeViewItemFocusVisual}"/> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type TreeViewItem}"> 
        <Grid> 
         <Grid.ColumnDefinitions> 
          <ColumnDefinition MinWidth="19" Width="Auto"/> 
          <ColumnDefinition Width="Auto"/> 
          <ColumnDefinition Width="*"/> 
         </Grid.ColumnDefinitions> 
         <Grid.RowDefinitions> 
          <RowDefinition Height="Auto"/> 
          <RowDefinition/> 
         </Grid.RowDefinitions> 
         <ToggleButton x:Name="Expander" ClickMode="Press" IsChecked="{Binding IsExpanded, RelativeSource={RelativeSource TemplatedParent}}" Style="{DynamicResource ToggleButtonStyle1}"/> 
         <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Grid.Column="1" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true"> 
          <ContentPresenter x:Name="PART_Header" ContentSource="Header" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/> 
         </Border> 
         <ItemsPresenter x:Name="ItemsHost" Grid.ColumnSpan="2" Grid.Column="1" Grid.Row="1"/> 
        </Grid> 
        <ControlTemplate.Triggers> 
         <Trigger Property="IsExpanded" Value="false"> 
          <Setter Property="Visibility" TargetName="ItemsHost" Value="Collapsed"/> 
         </Trigger> 
         <Trigger Property="HasItems" Value="false"> 
          <Setter Property="Visibility" TargetName="Expander" Value="Hidden"/> 
         </Trigger> 
         <Trigger Property="IsSelected" Value="true"> 
          <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/> 
          <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/> 
         </Trigger> 
         <MultiTrigger> 
          <MultiTrigger.Conditions> 
           <Condition Property="IsSelected" Value="true"/> 
           <Condition Property="IsSelectionActive" Value="false"/> 
          </MultiTrigger.Conditions> 
          <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/> 
          <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/> 
         </MultiTrigger> 
         <Trigger Property="IsEnabled" Value="false"> 
          <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/> 
         </Trigger> 
        </ControlTemplate.Triggers> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
     <Style.Triggers> 
      <Trigger Property="VirtualizingPanel.IsVirtualizing" Value="true"> 
       <Setter Property="ItemsPanel"> 
        <Setter.Value> 
         <ItemsPanelTemplate> 
          <VirtualizingStackPanel/> 
         </ItemsPanelTemplate> 
        </Setter.Value> 
       </Setter> 
      </Trigger> 
     </Style.Triggers> 
    </Style> 
    <Style x:Key="ToggleButtonStyle1" TargetType="{x:Type ToggleButton}"> 
     <Setter Property="Focusable" Value="False"/> 
     <Setter Property="Width" Value="16"/> 
     <Setter Property="Height" Value="16"/> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type ToggleButton}"> 
        <Border Background="Transparent" Height="16" Padding="5,5,5,5" Width="16"> 
         <Path x:Name="ExpandPath" Data="{StaticResource TreeArrow}" Fill="{DynamicResource {x:Static SystemColors.HotTrackBrushKey}}" Stroke="{StaticResource TreeViewItem.TreeArrow.Static.Stroke}"> 
          <Path.RenderTransform> 
           <RotateTransform Angle="135" CenterY="3" CenterX="3"/> 
          </Path.RenderTransform> 
         </Path> 
        </Border> 
        <ControlTemplate.Triggers> 
         <Trigger Property="IsChecked" Value="True"> 
          <Setter Property="RenderTransform" TargetName="ExpandPath"> 
           <Setter.Value> 
            <RotateTransform Angle="180" CenterY="3" CenterX="3"/> 
           </Setter.Value> 
          </Setter> 
          <Setter Property="Fill" TargetName="ExpandPath" Value="{StaticResource TreeViewItem.TreeArrow.Static.Checked.Fill}"/> 
          <Setter Property="Stroke" TargetName="ExpandPath" Value="{StaticResource TreeViewItem.TreeArrow.Static.Checked.Stroke}"/> 
         </Trigger> 
         <Trigger Property="IsMouseOver" Value="True"> 
          <Setter Property="Stroke" TargetName="ExpandPath" Value="{StaticResource TreeViewItem.TreeArrow.MouseOver.Stroke}"/> 
          <Setter Property="Fill" TargetName="ExpandPath" Value="{StaticResource TreeViewItem.TreeArrow.MouseOver.Fill}"/> 
         </Trigger> 
         <MultiTrigger> 
          <MultiTrigger.Conditions> 
           <Condition Property="IsMouseOver" Value="True"/> 
           <Condition Property="IsChecked" Value="True"/> 
          </MultiTrigger.Conditions> 
          <Setter Property="Stroke" TargetName="ExpandPath" Value="{StaticResource TreeViewItem.TreeArrow.MouseOver.Checked.Stroke}"/> 
          <Setter Property="Fill" TargetName="ExpandPath" Value="{StaticResource TreeViewItem.TreeArrow.MouseOver.Checked.Fill}"/> 
         </MultiTrigger> 
        </ControlTemplate.Triggers> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 

</Window.Resources> 
<Grid> 
    <StackPanel Orientation="Horizontal"> 
    <TreeView x:Name="XX" HorizontalAlignment="Left" Height="100" VerticalAlignment="Top" Width="100" Style="{DynamicResource TreeViewStyle1}" Background="#7B898C"> 
     <TreeViewItem Header="XXXX_1" Style="{StaticResource TreeViewItemStyle1}"> 
      <TreeViewItem Header="XXXX_2" HorizontalAlignment="Left" /> 
     </TreeViewItem> 
    </TreeView> 

    <TreeView x:Name="XX2" Margin="10,0,0,0" HorizontalAlignment="Left" Height="100" VerticalAlignment="Top" Width="100" Style="{DynamicResource TreeViewStyle1}" Background="#7B898C"> 
     <TreeViewItem Header="XXXX_3" Style="{StaticResource TreeViewItemStyle1}"> 
      <TreeViewItem Header="XXXX_4" HorizontalAlignment="Left" /> 
     </TreeViewItem> 
    </TreeView> 
    </StackPanel> 
</Grid> 

답변

0

유일한 것은 당신이 게시 한 코드에서 잘못된 볼 수있다 (임 뒤에 코드에서 트 리뷰 컨트롤을 변경하지 않음)인가하면 TriggerSetter 세트 모두 Background입니다 같은 색상 Foreground{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}

또한 어쩌면 명시 적으로 Selector.IsSelectionActiveMultiTrigger 조건을 설정하려고 수 :

나는 이러한 변화와 함께 게시 된 코드를 시도하는 것은 (-8 Windows에서 나를 위해 잘 작동) :

<MultiTrigger> 
    <MultiTrigger.Conditions> 
    <Condition Property="IsSelected" 
       Value="true" /> 
    <Condition Property="Selector.IsSelectionActive" 
       Value="false" /> 
    </MultiTrigger.Conditions> 
    <Setter TargetName="Bd" 
      Property="Background" 
      Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" /> 
    <Setter Property="Foreground" 
      Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}" /> 
</MultiTrigger> 

출력 :

enter image description here

:

당신 만 Style 설정 상위 TreeViewItemTreeViewItemStyle1입니다. 따라서 기본값 StyleStyle을 지정하지 않는 자식 TreeViewItem에 적용됩니다. TreeViewItem의 모든 문자에 자동으로 적용하려면 스타일 키를 선언에서 제거하고 TargetType

+0

을 기반으로 자동으로 적용해야합니다. 문제 해결됨. 여기서 두 가지가 핵심입니다. 첫 번째 것은 Property = "Selector.IsSelectionActive"이고 두 번째는 모든 treeview 항목에 정의 된 스타일이 있는지 확인하는 것입니다. – programstait