C#MVVM:将单选按钮绑定到布尔属性 [英] C# MVVM: Binding a RadioButton to a boolean Property
问题描述
我是编程新手,现在正在学习C#和MVVM模式.
I am quiet new to programming and am currently learning C# and the MVVM pattern.
我需要为大学的ChiliPlants编写一个数据库工具.在那里,您应该可以向ObservableCollection添加新对象.
I need to code a database tool for ChiliPlants for university. There you should be able to add a new object to an ObservableCollection.
要将新项目添加到此ObservableCollection,将打开一个新窗口.看起来像这样:窗口添加
To add a new Item to this ObservableCollection a new Window opens. It looks like this: Window Add
我现在希望将两个RadioBox绑定到一个名为"HybridSeed"的属性.在ViewModel中定义的:
I now want the two RadioBoxes to be bound to a property called "HybridSeed". Which is defined in the ViewModel:
//Public Property HybridSeed
public bool HybridSeed
{
get { return ChiliModel.HybridSeed; }
set
{
if (ChiliModel.HybridSeed == value)
return;
ChiliModel.HybridSeed = value;
OnPropertyChanged("HybridSeed");
}
}
我的视图的RadioBox部分如下所示:
The RadioBox part of my View looks like this:
<RadioButton Grid.Row="5" Content="Ja" Grid.Column="1" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top"/>
<RadioButton Grid.Row="5" Content="Nein" Grid.Column="1" HorizontalAlignment="Left" Margin="89,10,0,0" VerticalAlignment="Top"/>
但是如何将用户单击这些RadioButton的结果绑定到此HybridSeed属性?重要的是结果是傻瓜.
But how to bind the outcome of a user clicking on these RadioButtons to this HybridSeed Property? Important is that the outcome is a bool.
我几乎查找了与该主题相似的所有条目,但没有找到简单的解决方案.或是我用不良的编码技巧就能理解的解决方案:( ...
I looked up almost every entry similar to this topic, but I did not find a simple solution. Or a solution which I was able to understand with my bad coding skills :( ...
如果你们能帮助我,我会很高兴.对于这个新手,请保持简单:)
I would be very happy if you guys could help me. Please keep it simple for this newbie :)
如果有使用CheckBox或ComboBox的更简单的解决方案,那也将是完美的.最重要的是拥有一个不错的用户界面.现在,它仅适用于用户始终必须写"True"或"False"的TextBox.
If there is a simpler solution using a CheckBox or a ComboBox it would also be perfect. The most important thing is to have a nice user interface. Right now it only works with a TextBox where the user always has to write "True" or "False".
解决方案:
我在是"单选按钮中添加了IsClicked属性,以便通过以下方式将其绑定到我的boulean属性:IsClicked ="{Binding HybridSeed}".感谢naslund的快速回答:)
I added the IsClicked Property in the "Yes" RadioButton to be bound to my boulean property with: IsClicked="{Binding HybridSeed}". Thanks to naslund for his fast answer :)
推荐答案
只需将HybridSeed绑定到Yes-radiobutton.然后,如果用户选择了它,则为true;如果选择了否"单选按钮(或者如果未选择任何内容),则为false.在这种情况下,绑定到两个按钮有点多余,因为单选按钮的机制可以解决这个问题.
Just bind HybridSeed to the Yes-radiobutton. It will then either be true if the user has selected that or false if No-radiobutton has been selected (or if nothing has been selected). Binding to both buttons in this case is a bit redundant since the mechanism of radiobuttons takes care of it.
WPF:
<RadioButton Content="Yes" IsChecked="{Binding HybridSeed}" />
<RadioButton Content="No" />
<Label Content="{Binding HybridSeed}" ContentStringFormat="Value is: {0}" />
逻辑:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new ViewModel();
}
}
public class ViewModel : INotifyPropertyChanged
{
private bool hybridSeed;
public bool HybridSeed
{
get { return hybridSeed; }
set
{
hybridSeed = value;
OnPropertyChanged(nameof(HybridSeed));
}
}
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
这篇关于C#MVVM:将单选按钮绑定到布尔属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!