在我的程序中,我有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"
}
}
}
};
我希望这些项目在选中时显示WHITE 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
?
答案 0 :(得分: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>
有关详细信息,请参阅:
Styling and Templating on MSDN
Example of TreeView Style/Template on MSDN
<强> 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
模板,则可以缩短此代码并使其更加轻松。