How to Write Triggers and Setters for a TreeView through C# rather than XAML

喜你入骨 提交于 2019-12-12 02:52:43

问题


How to Add following XAML based trigger to TreeView from Code behind rather than XAML.

<TreeView>
        <TreeView.Resources>
            <Style TargetType="{x:Type TreeViewItem}">
                <Setter Property="ContextMenu">
                    <Setter.Value>
                        <ContextMenu>
                            <MenuItem Header="Menu Item 1" />
                            <MenuItem Header="Menu Item 2" />
                        </ContextMenu>
                    </Setter.Value>
                </Setter>
            </Style>
        </TreeView.Resources>
        <TreeViewItem Header="Item 1">
            <TreeViewItem Header="Sub-Item 1"/>
        </TreeViewItem>
        <TreeViewItem Header="Item 2"></TreeViewItem>
    </TreeView>

WPF's default behavior is to change the TreeViewItem to gray when the ContextMenu opens, but like virtually everything else in WPF you can override this:

Create an attached property ContextMenuOpened In the TreeViewItem Style, bind ContextMenuOpened to "ContextMenu.IsOpen" Add a trigger that changes the brush when ContextMenuOpened and IsSelected are both true Here's the attached property:

public class TreeViewCustomizer : DependencyObject
{
  public static bool GetContextMenuOpened(DependencyObject obj) { return (bool)obj.GetValue(ContextMenuOpenedProperty); }
  public static void SetContextMenuOpened(DependencyObject obj, bool value) { obj.SetValue(ContextMenuOpenedProperty, value); }
  public static readonly DependencyProperty ContextMenuOpenedProperty = DependencyProperty.RegisterAttached("ContextMenuOpened", typeof(bool), typeof(TreeViewCustomizer));
}

Here's the setter in the style:

<Setter Property="my:TreeViewCustomizer.ContextMenuOpened"
        Value="{Binding ContextMenu.IsOpen, RelativeSource={RelativeSource Self}}" />

Here's the trigger:

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

All trees in my application are created at Runtime through C# code. I want to do all the above work throughC# code as I have created my tree at Runtime by using following code

TreeView _objTreeView =  new TreeView();

Reference Question : WPF TreeViewItem Context Menu Unhighlights Item


回答1:


Done it using following statements

SolidColorBrush colorBrush = new SolidColorBrush(Colors.DodgerBlue); myTreeView.Resources.Add(SystemColors.ControlBrushKey, colorBrush);

:)



来源:https://stackoverflow.com/questions/8969139/how-to-write-triggers-and-setters-for-a-treeview-through-c-sharp-rather-than-xam

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!