3

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

像这样:

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在被选中时显示为白色(就像默认节点行为一样)。

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

<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

4

1 回答 1

2

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

<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>

欲了解更多信息,请参阅:

MSDN 上的样式和模板

MSDN 上的 TreeView 样式/模板示例

EDIT

尝试这个:

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;         
}

Note:如果您使用TreeViewItem.

于 2013-08-19T18:52:11.177 回答