在 ItemsControl 内绑定 RadioButton 的 IsChecked 属性
我有 ItemsControls 和从 CollectionViewSource 绑定的项目。
<ItemsControl ItemsSource="{Binding Source={StaticResource VisibleFlagsImageSourcePathView}}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<r:RibbonRadioButton SmallImageSource="{Binding}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
另一个控制外站:
<TextBox Text="{Binding Path=SelectedCountryCode" />
我想要完成的是每当我更改 TextBox 的值时,我希望相应的 RibbonRadioButton 属性 IsChecked 设置为 true 或 false。
I have ItemsControls with items binded from CollectionViewSource.
<ItemsControl ItemsSource="{Binding Source={StaticResource VisibleFlagsImageSourcePathView}}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<r:RibbonRadioButton SmallImageSource="{Binding}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
And another control outsite:
<TextBox Text="{Binding Path=SelectedCountryCode" />
What I am trying to accomplish is whenever I change the value of the TextBox I want the corresponding RibbonRadioButton property IsChecked set to true or false.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要做的是创建一个具有两个属性的
ViewModel
。并监视
SelectedCountryCode
,只要它发生变化,就更改VisibleFlagsImageSourcePath
集合中的适当值。What you need to do is create a
ViewModel
with two properties.And monitor the
SelectedCountryCode
, and whenever it changes, change appropriate value inVisibleFlagsImageSourcePath
collection.单选按钮代表枚举值。在这种情况下,文本框将代表一个开放值。您似乎想要的是一组开放值以及枚举值的预设选择。最能代表这一点的控件是组合框。
如果您决定继续使用单选按钮/文本框方法,则可以调整人们用于将单选按钮绑定到枚举值的方法,除了使用字符串字段/字符串字段类型转换器而不是枚举字段/枚举字段类型转换器。
有关如何绑定到枚举的信息,请参阅此答案:如何将 RadioButtons 绑定到枚举?
要使其适应字符串,只需创建一个名为
KnownStringToBooleanConverter
的类(请注意,这是与EnumToBooleanConverter
相同的实现):同时创建具有已知字符串的类型(类似于创建枚举的方式):
然后以类似的方式绑定到此:
如果您希望所有控件交叉填充,那么您需要实现
INotifyPropertyChanged 在视图模型中:
当输入自定义值(不在列表中)时,单选按钮将全部变暗。当您输入列表中的值时,相应的单选按钮将亮起。当您选择正确的单选按钮时,文本框中的值将发生更改。
这种 View/ViewModel 的东西称为 MVVM。
请参阅:http://msdn.microsoft.com/en-us/magazine/ dd419663.aspx
Radio buttons represent enumerated values. A text box in this case would represent an open value. What you seem to want is a set of open values as well as a pre-set selection of enumerated values. The control that best represents this is a combo box.
If you decide to continue with the radio button/text box approach, you can adapt the method people use to bind radio buttons to an enumerated value, except use a string field/string field type converter instead of an enum field/enum field type converter.
See this answer for how to bind to enums: How to bind RadioButtons to an enum?
To adapt this to strings, simply make a class called
KnownStringToBooleanConverter
(note that this is an identical implementation toEnumToBooleanConverter
):Also create a type with your known strings (similar to how you would create an enum):
Then bind to this in a similar way:
If you want all your controls to cross-populate, then you'll need to implement
INotifyPropertyChanged
in your view model:When a custom value (that isn't in the list) is entered, the radio buttons will all dim. When you type in a value that is from the list, the corresponding radio button will light up. When you select a correct radio button, the value will be changed in the text box.
This View/ViewModel stuff is called MVVM.
See: http://msdn.microsoft.com/en-us/magazine/dd419663.aspx