Click here to Skip to main content
15,898,035 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am working on an application for a company - cannot disclose full information on the project but I will try and be descriptive enough. I have a WPF project (C#), I use MVVM, where a user opens the application , makes a selection on a combobox, checks Togglebuttons and saves that link by pressing a Button.
For your understanding I will make an example of a team pf players. Combobox has Team A,B,C and the Togglebuttons are the names of the players. User selects Team A and selects players John and Dave for that team and presses save - this is saved to an xml file. When the Togglebutton is checked, it changes color. Now when the user opens the App again, selects Team A from the Combobox, the players that were already selected should appear as checked - change color.How can I achieve this using MVVM? The ViewModel side works fine - I can get the names of players that are linked to current selection of Combobox but I don't know how to bind this to the Togglebutton.
P.S. this link http://www.dotnetlead.com/wpf-organization-chart/application-and-source-code is an example I took this from, it will help you understand what I'm talking about - the selection of the Togglebutton especially. I do not have multiple Togglebuttons in my .xaml, but only one...the others are created at runtime depending on the player list. Please follow link to understand what I mean here...

What I have tried:

At this point I just need a theoretical answer suggesting what I can try - does not have to be code. I know it's something I have to do in the XAML because like I said I have the logic in the VM to get the players linked to a team...The problem is setting the Togglebutton to checked without actually clicking on it...
Posted
Updated 21-Feb-17 0:52am

give the Combobox a name then on the ToggleButton:
C#
<ToggleButton Content={Binding ElementName=ComboBoxName, Path=SelectedIndex}"/>
or to whatever property that needs binding.
 
Share this answer
 
Thanks for the prompt reply. Here is what my Togglebutton looks like currently. I am already binding it to something, so is it possible to bind it to something else?


<ToggleButton x:Name="btnPlayers" Template="{StaticResource ctpDetailButton}" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Command="{Binding SelectedCommand, Mode=TwoWay}" IsChecked="{Binding IsChecked,Mode=TwoWay,UpdateSourceTrigger=Explicit}" >

**you can ignore other properties here. Another thing, the combobox is in a different UserControl -it's in the MainWindow.xaml
 
Share this answer
 
Comments
Graeme_Grant 21-Feb-17 6:56am    
What is the purpose of binding the ComboBox.SelectedIndex to the ToggleButton? Then I can give a better answer.
AvuMxl 21-Feb-17 7:01am    
The requirement is that when an item in the combobox is selected, the togglebutton with the names of the players that are linked to the combobox item (team) should be checked. So i thought linking the selection of the the combobox is what I need here but a different solution is accepted too...What I cannot do is have to press a button to show this link, this should happen "automatically" after the user selects a team from the combobox. I hope this is clearer...
Graeme_Grant 21-Feb-17 7:07am    
You're binding to IsChecked in the Model (ViewModel, Model or code-behind). So you have two choices:
1. Bind the Selected index to the Model, and let the model handle the logic; or
2. Use a MultiValueConverter[^] and let it do the logic.
AvuMxl 21-Feb-17 7:26am    
I think I will go with option 1 - I have never used 2 before.
In my VM I already have logic that reads the .xml file, looks for the players that are linked to the team and for now I display them in a Messagebox just for testing purposes. So when you say I should bind SelectedIndex you mean bind the Method I have this logic in to the selectedIndex? if so, how do I make the link to the Togglebutton then because that is what is left now?
Graeme_Grant 21-Feb-17 7:28am    
Bind the SelectedIndex to a property in your VM. Then when the property is set, apply your logic. This also gives you control of the ComboBox if you need to manually set/clear the selection. SelectedIndex = -1 is to remove selection.

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