当我单击TreeViewItem时,将调用ItemClicked()。
C#:
public void ItemClicked(object sender, RoutedEventArgs e)
{
if (sender is TreeViewItem ClickedNode)
{
if (ClickedNode.HasItems) // Its a parent node
{
if (CtrlPressed)
{
var SelectedChild = TreeView.SelectedItem as TreeViewItem; // Have to do this manually since the sender is parent???
SelectedChild.Background = Brushes.Black;
SelectedChild.Foreground = Brushes.White;
}
}
}
在我的XAML中,我禁用了IsSelected默认颜色:
<Grid>
<TreeView Name="TreeView">
<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
<EventSetter Event="MouseLeftButtonUp"
Handler="ItemClicked"/>
</Style>
</TreeView.ItemContainerStyle>
<TreeView.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"
Color="Transparent" />
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}"
Color="Black" />
<SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}"
Color="Transparent" />
<SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightTextBrushKey}"
Color="Black" />
</TreeView.Resources>
</TreeView>
</Grid>
因此,现在当我按住Control键单击节点时,它不会以黑色显示,而是全白显示。因此,透明似乎覆盖了刚刚设置的Brushes.Black。前景颜色似乎会穿过。
第一个问题:发件人不是实际单击的节点,而是其父节点。这是为什么?我必须手动找出它是否是父节点,然后再手动获得.SelectedItem。我该如何解决?
第二个问题:如何解决颜色的替代问题?当IsSelected为true时,我什么都不想要...