繁体   English   中英

在 WPF 树视图中单击时如何更改 TreeViewItem 的背景颜色?

[英]How to change the background color of TreeViewItem when clicking in the WPF treeview?

我想在单击 TextBlock 时更改 StackPanel 的背景。

在此处输入图片说明

现在背景是蓝色! 我想改变它!

<TreeView  Name="tvView" HorizontalAlignment="Left"  VerticalAlignment="Top"   BorderThickness="0">

        <TreeView.ItemTemplate>
            <HierarchicalDataTemplate ItemsSource="{Binding Path=ChildNodes}">
                <Grid VerticalAlignment="Top" Margin="0">
                    <StackPanel Orientation="Horizontal" d:LayoutOverrides="Height">
                        <TextBlock Name="ConfidenceLevelReminderText" Text="!" FontWeight="Bold" HorizontalAlignment="Center" Foreground="#FF0000" Width="{Binding Path=ConfidenceLevelWidth}" Margin="0,0,0,0"></TextBlock>
                        <TextBlock TextWrapping="Wrap" 
                                   Padding="0,3,0,3" 
                                   Text="{Binding Path=Name}" 
                                   ToolTip="{Binding Path=NameToolTip}" 
                                   Tag="{Binding Path=TagInfo}" 
                                   MouseLeftButtonDown="name_MouseLeftButtonDown" 
                                   FontSize="14" 
                                   VerticalAlignment="Center" 
                                   HorizontalAlignment="Center" 
                                   Foreground="#FF666666" 
                                   Margin="0" 
                                   Width="{Binding Path=TextWidth}" 
                                   Cursor="Hand"/>
                        <Button x:Name="btnIngnore" Width="{Binding Path=IgnoreWidth}" Click="btn_ignore_Click" Tag="{Binding Path=NodeId}" Margin="0"  BorderThickness="0" Background="{x:Null}" Style="{StaticResource ButtonStyle32}" IsEnabled="{Binding Path=IgnoreWidth,Converter={StaticResource itb}}"  Cursor="Hand">
                            <TextBlock Text="xxxx" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0" FontSize="12" Foreground="#FF666666"/>
                        </Button>

                    </StackPanel>
                </Grid>
            </HierarchicalDataTemplate>
        </TreeView.ItemTemplate>
    </TreeView>

如果你想

单击 TextBlock 时更改 StackPanel 的背景。

您可以将 EventSetter 添加到TextBlock的 Style 并在事件处理程序中处理事件。

<TextBlock Text="...">
    <TextBlock.Style>
        <Style TargetType="TextBlock">
            <EventSetter Event="MouseDown" Handler="Mouse_LBtnDown"/>
        </Style>
    </TextBlock.Style>
</TextBlock>

private void Mouse_LBtnDown(object sender, MouseButtonEventArgs e)
{
    StackPanel stp = null;
    var visParent = VisualTreeHelper.GetParent(sender as FrameworkElement);
    while (stp == null && visParent != null)
    {
        stp = visParent as StackPanel;
        visParent = VisualTreeHelper.GetParent(visParent);
    }
    if (stp == null) { return; }

    stp.Background = Brushes.Coral;
}

为了使其可重用和可调,您可以从事件处理程序中创建一个行为。

如果你不想要你所问的问题,那么重新思考你的问题并提出新的问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM