Click here to Skip to main content
15,888,113 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone,
I am fairly new with using WPF and I jumped right in and used the MVVM pattern. I have a project where I need to set IsChecked = true for nested toggle button i.e. toggle button inside a toggle button.
Background: I am doing an app to display an organizational structure so at run time the toggle buttons get created. I used toggle buttons because they show the hierarchy nicely plus I needed the state - when the button is checked, change color and add to list somewhere (ignore this part). This works perfectly when I click on the parent node but what I want is for the child nodes within that parent node to be changed as well.

N.B. there is only one togglebutton in my XAML

P.S. The "base idea" for this project is taken from Hierarchy Application - SharePoint[^]

What I have tried:

<togglebutton x:name="btnNodes" template="{StaticResource ctpDetailButton}" command="{Binding SelectedCommand}" ischecked="{Binding IsSelected,Mode=TwoWay,UpdateSourceTrigger=Explicit}">
ViewModel: I have a property "IsSelected" and I set it to true on PropertyChanged" (exactly as done in the link. Then I loop through the children nodes to set isSelected to true. This works but it does not change the background color for the toggle buttons inside the "parent" toggle button.
Posted
Updated 26-Oct-16 2:06am
v3

1 solution

The binding for the inner button's IsSelected property merely needs to refer to the value of it's parent control's IsSelected property, something like this:

XAML
"IsSelected={Binding IsSelected, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ToggleButton}}}"
 
Share this answer
 
v2
Comments
AvuMxl 26-Oct-16 7:26am    
Thanks for the prompt reply. I have just one toggle button in my view...the others get created at run time according to the "Children" property in my viewmodel. So adding those lines as you suggested did not make any difference, only the parent gets the background change...
#realJSOP 26-Oct-16 7:49am    
Then improve your question. When you say "toggle button in a toggle button", I am assuming that you're setting the toggle buttons Content property to another toggle button. If that's NOT what you're doing, then what I suggested will of course not work.
AvuMxl 26-Oct-16 8:07am    
Okay thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900