【问题标题】:how to write C# equivalent code corresponding to this XAML code for customizing selected treeviewItem color如何编写与此 XAML 代码对应的 C# 等效代码以自定义选定的 treeviewItem 颜色
【发布时间】:2012-02-17 06:23:21
【问题描述】:
<TreeView.Resources>
    <SolidColorBrush Color="Transparent" x:Key="{x:Static SystemColors.HighlightBrushKey}"/>
 </TreeView.Resources>

实际上,我在 WPF 应用程序中有一个 TreeView,因为它包含各种 TreeViewItems ,当我将一个树视图项拖到 DataGrid 时,TreeViemItem 变灰。

由于我没有使用 XAML 创建树视图,因此我使用了 C# 代码,因此我只想使用 C# 代码来修复它。

TreeView treeNode = new TreeView();

//In some loop
TreeViewItem childTreeNode = new TreeViewItem();
childTreeNode.Header = "Item 1";
childTreeNode.ToolTip = "File Path";

childTreeNode.Foreground = Brushes.Black;
childTreeNode.Background = Brushes.White;

treeNode.Items.Add(childTreeNode);
//End Loop

现在很少有 TreeView 项被添加到这个 TreeView 中。 树看起来不错,但是一旦从树上移除焦点,它被选中的树视图项就会变灰

也参考这个问题 How to Write Triggers and Setters for a TreeView through C# rather than XAML

【问题讨论】:

    标签: c# .net wpf xaml treeview


    【解决方案1】:

    你可以这样做。

    var colorBrush = new SolidColorBrush(Colors.Transparent);
    treeNode.Resources.Add(SystemColors.HighlightBrushKey, colorBrush);
    

    【讨论】:

    • 感谢使用类似的语句完成它 SolidColorBrush colorBrush = new SolidColorBrush(Colors.DodgerBlue); treeNode.Resources.Add(SystemColors.ControlBrushKey, colorBrush);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-11
    • 1970-01-01
    • 2020-12-09
    • 2017-12-18
    相关资源
    最近更新 更多