C# MVVM:将 RadioButton 绑定到布尔属性布尔、绑定、属性、MVVM

2023-09-07 09:26:06 作者:伤心了就哭吧

我对编程很陌生,目前正在学习 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"/>

但是如何将用户点击这些 RadioButtons 的结果绑定到这个 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 的更简单的解决方案,它也将是完美的.最重要的是有一个漂亮的用户界面.现在它只适用于用户总是必须写真"或假"的文本框.

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 属性以绑定到我的布尔属性: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;如果选择了 No-radiobutton(或未选择任何内容),则为 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));
    }
}