选择时更改 TreeViewItem 的前景 [英] Changing Foreground of a TreeViewItem upon selection

查看:32
本文介绍了选择时更改 TreeViewItem 的前景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的程序中,我有一个 TreeView,用户可以从中选择不同的项目.我的 TreeView 中有一些项目是在我的 c# 代码隐藏中创建时自定义的.

In my program I have a TreeView in which the user will select different items from. There are a few items in my TreeView that are customized upon creation in my c# code-behind.

像这样:

public static TreeViewItem newItem = new TreeViewItem() //Child Node
{
       Header = new StackPanel //ICON
       {
           Orientation = Orientation.Horizontal,
           Children =
           {
               new Border {
                   Width = 12,
                   Height = 14,

                   Background = Brushes.Blue,
                   BorderThickness = new Thickness(1.0),
                   BorderBrush = Brushes.Black
              },
              new Label {
                  Content = "Node1"
              }
           }
      }
};

我希望这些项目在被选中时显示白色 foregrounds(就像默认节点行为一样).

I would like these items to display WHITE foregrounds when they are selected (just like the default node behavior).

这是我迄今为止在 XAML 中尝试过的.这是我为 TreeViewItems 设置的样式模板.我没有收到编译器错误,但由于某种原因,当我运行程序时,我的 TreeView 不可见.

This is what I have tried so far in XAML. It is a style template that I have set for TreeViewItems. I receive no compiler errors, but for some reason when I run the program my TreeView is not visible.

<Style TargetType="{x:Type TreeViewItem}" >
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TreeViewItem}">
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsSelected" Value="True" >
                                <Setter Property="Foreground" Value="White" />
                            </Trigger>
                            <Trigger Property="IsSelected" Value="False" >
                                <Setter Property="Foreground" Value="Black" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
</Style>

如何解决这个问题,以便我的所有 TreeView 节点在被选中时都显示白色的 foregrounds?

How can I fix this so that all of my TreeView nodes display white foregrounds when selected?

推荐答案

这是因为你完全重写了模板,而你没有写任何东西.只是设置触发器,不一定要在模板中设置,您可以在Style 中设置它们.模板通常设置为更改可视化树中的元素.试试这个例子:

This is because you completely rewrite the template, and you do not write anything instead. Just to set the triggers, not necessarily to do them in the template, you can just set them in Style. Template is typically set to change the elements in the visual tree. Try this example:

<Window.Resources>
    <Style TargetType="{x:Type TreeViewItem}">
        <Style.Resources>
            <!-- Set Highlight Background color -->
            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Black" />
        </Style.Resources>

        <Style.Triggers>
            <Trigger Property="IsSelected" Value="True">
                <!-- Set Foreground color -->
                <Setter Property="Foreground" Value="White" />
            </Trigger>
        </Style.Triggers>
    </Style>
</Window.Resources>

<Grid>
    <TreeView>
        <TreeViewItem Header="Root">
            <TreeViewItem Header="Child1" />
            <TreeViewItem Header="Child2" />
            <TreeViewItem Header="Child3" />
            <TreeViewItem Header="Child4" />
        </TreeViewItem>
    </TreeView>
</Grid>

有关更多信息,请参阅:

For more information, please see:

MSDN 上的样式和模板

MSDN 上的 TreeView 样式/模板示例

编辑

试试这个:

public TreeViewItem newItem = new TreeViewItem() //Child Node
{
    Header = new StackPanel 
    {
        Orientation = Orientation.Horizontal,

        Children =
        {
            new Border 
            {
                Width = 12,
                Height = 14,

                Background = Brushes.Blue,
                BorderThickness = new Thickness(1.0),
                BorderBrush = Brushes.Black
            },

            new Label 
            {
                Content = "Node1",
                Foreground = Brushes.Black,
            }
        }
    }
};

private void AddItem_Click(object sender, RoutedEventArgs e)
{
    // Set Selected handler on Selected event
    newItem.Selected += new RoutedEventHandler(newItem_Selected);

    // Set Unselected handler on Unselected event
    newItem.Unselected += new RoutedEventHandler(newItem_Unselected);

    // Add your item
    MyTreeView.Items.Add(newItem);
}

// Set the black color for foreground
private void newItem_Unselected(object sender, RoutedEventArgs e) 
{
    TreeViewItem MyTreeViewItem = sender as TreeViewItem;
    StackPanel MyStackPanel = MyTreeViewItem.Header as StackPanel;
    Label MyLabel = MyStackPanel.Children[1] as Label;

    MyLabel.Foreground = Brushes.Black;
}

// Set the white color for foreground
private void newItem_Selected(object sender, RoutedEventArgs e)
{
    TreeViewItem MyTreeViewItem = sender as TreeViewItem;
    StackPanel MyStackPanel = MyTreeViewItem.Header as StackPanel;
    Label MyLabel = MyStackPanel.Children[1] as Label;

    MyLabel.Foreground = Brushes.White;         
}

注意: 如果您使用 TreeViewItem 的模板,可以缩短此代码并使其更容易.

Note: This code can be shortened and made easier if you use a template for TreeViewItem.

这篇关于选择时更改 TreeViewItem 的前景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆