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










2















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>









share|improve this question
























  • 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.html

    – pjrki
    Nov 12 '18 at 10:05















2















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>









share|improve this question
























  • 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.html

    – pjrki
    Nov 12 '18 at 10:05













2












2








2








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>









share|improve this question
















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>






c# wpf treeview






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 '18 at 8:44









Rekshino

2,7961728




2,7961728










asked Nov 12 '18 at 8:30









yi muyi mu

213




213












  • 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.html

    – pjrki
    Nov 12 '18 at 10:05

















  • 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.html

    – pjrki
    Nov 12 '18 at 10:05
















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

– pjrki
Nov 12 '18 at 10:05





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

– pjrki
Nov 12 '18 at 10:05












1 Answer
1






active

oldest

votes


















1














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.






share|improve this answer























  • I want all nodes to have only one background change at the same time, which indicates that it has been selected and that the background of the previous node will not disappear.

    – yi mu
    Nov 13 '18 at 0:43










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53258315%2fhow-to-change-the-background-color-of-treeviewitem-when-clicking-in-the-wpf-tree%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














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.






share|improve this answer























  • I want all nodes to have only one background change at the same time, which indicates that it has been selected and that the background of the previous node will not disappear.

    – yi mu
    Nov 13 '18 at 0:43















1














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.






share|improve this answer























  • I want all nodes to have only one background change at the same time, which indicates that it has been selected and that the background of the previous node will not disappear.

    – yi mu
    Nov 13 '18 at 0:43













1












1








1







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.






share|improve this answer













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.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 12 '18 at 13:46









RekshinoRekshino

2,7961728




2,7961728












  • I want all nodes to have only one background change at the same time, which indicates that it has been selected and that the background of the previous node will not disappear.

    – yi mu
    Nov 13 '18 at 0:43

















  • I want all nodes to have only one background change at the same time, which indicates that it has been selected and that the background of the previous node will not disappear.

    – yi mu
    Nov 13 '18 at 0:43
















I want all nodes to have only one background change at the same time, which indicates that it has been selected and that the background of the previous node will not disappear.

– yi mu
Nov 13 '18 at 0:43





I want all nodes to have only one background change at the same time, which indicates that it has been selected and that the background of the previous node will not disappear.

– yi mu
Nov 13 '18 at 0:43

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53258315%2fhow-to-change-the-background-color-of-treeviewitem-when-clicking-in-the-wpf-tree%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo