如何将combobx的selectedindex绑定到togglebutton C# [英] How do I bind selectedindex of combobx to togglebutton C#

查看:74
本文介绍了如何将combobx的selectedindex绑定到togglebutton C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为一家公司申请 - 无法透露有关该项目的完整信息,但我会尝试描述性。我有一个WPF项目(C#),我使用MVVM,用户打开应用程序,在组合框中进行选择,检查Togglebuttons并按下按钮保存该链接。

为了您的理解我将举例说明球队的球员。 Combobox有A,B,C队,Togglebuttons是队员的名字。用户选择A队并为该队选择球员John和Dave并按下保存 - 这将保存到xml文件中。选中Togglebutton时,它会改变颜色。现在,当用户再次打开应用程序时,从Combobox中选择Team A,已经选中的玩家应该显示为已选中 - 更改颜色。如何使用MVVM实现此目的? ViewModel端工作正常 - 我可以获得与当前选择的Combobox相关联的玩家的名字,但我不知道如何将其绑定到Togglebutton。

P.S.这个链接http://www.dotnetlead.com/wpf-organization-chart/application-and-source-code是我从中得到的一个例子,它将帮助你理解我在说什么 - 选择Togglebutton特别。我的.xaml中没有多个Togglebutton,但只有一个...其他的是在运行时创建的,具体取决于播放器列表。请按照链接了解我在这里的意思...



我尝试过:



此时我只需要一个理论答案,建议我可以尝试 - 不必是代码。我知道这是我在XAML中必须做的事情,因为就像我说我在VM中有逻辑来让玩家链接到一个团队......问题是设置Togglebutton进行检查而不实际点击它......

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

推荐答案

然后在ToggleButton上为Combobox命名:
give the Combobox a name then on the ToggleButton:
<ToggleButton Content={Binding ElementName=ComboBoxName, Path=SelectedIndex}"/>

或任何需要绑定的属性。

or to whatever property that needs binding.


感谢您的快速回复。这是我的Togglebutton目前的样子。我已经将它绑定到了某个东西,所以可以将它绑定到其他东西吗?





< ToggleButton x:Name =btnPlayersTemplate ={StaticResource ctpDetailButton}Horizo​​ntalContentAlignment =CenterVerticalContentAlignment =CenterCommand ={Binding SelectedCommand, Mode = TwoWay}IsChecked ={绑定IsChecked,M ode = TwoWay,UpdateSourceTrigger = Explicit}>



**你可以忽略其他属性。另一件事,组合框在MainWindow.xaml中的另一个UserControl中 -
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

这篇关于如何将combobx的selectedindex绑定到togglebutton C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆