2
votes

I want to change the background of StackPanel when clicking TextBlock.

enter image description here

now background is bule! I want to change it!

<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>
1
Read about control templates and try to create new style for treeviewitem with some triggers. For example if Trigger is selected then change your color. If you will have some issues, just let me know. wpftutorial.net/templates.htmlpjrki

1 Answers

1
votes

If you want

to change the background of StackPanel when clicking TextBlock.

You can add EventSetter to the TextBlock's Style and handle event in the event handler.

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

For make it reusable and adjustable you could make a behavior from the event handler.

If you don't want what you have asked, then rethink your question and ask new one.