如果检查组的单选按钮,我该怎么办?

时间:2018-01-03 13:35:24

标签: c# wpf

enter image description here


这是XAML:

<Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="auto"></RowDefinition>
            <RowDefinition Height="auto"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>
        <StackPanel>
            <RadioButton GroupName="a">123</RadioButton>
            <RadioButton GroupName="a">456</RadioButton>
            <RadioButton GroupName="a">789</RadioButton>
        </StackPanel>
        <Button Grid.Row="1" IsEnabled="False">Sumit</Button>
    </Grid>

如您所见,这里有一组radiobutton和一个sumit按钮。

我想这样做,如果没有检查组中的单选按钮,则无法执行sumit按钮,如果检查了组中的一个radiobutton,则启用sumit按钮。

我该怎么办?你想帮助我吗?谢谢你。

3 个答案:

答案 0 :(得分:3)

完成此任务的最佳方法是使用MVVM。您可以创建一个可以绑定到UI元素的ViewModel对象。然后你可以创建一个特定的属性来检查其中任何一个按钮:

<StackPanel>
    <RadioButton GroupName="a" IsChecked="{Binding R1}">123</RadioButton>
    <RadioButton GroupName="a" IsChecked="{Binding R2}">456</RadioButton>
    <RadioButton GroupName="a" IsChecked="{Binding R3}">789</RadioButton>
</StackPanel>
<Button Grid.Row="1" IsEnabled="{Binding SubmitEnabled}">Sumit</Button>

您的ViewModel如下所示:

class ViewModel : INotifyPropertyChanged
{
    private bool r1 = false;
    private bool r2 = false;
    private bool r3 = false;

    public event PropertyChangedEventHandler PropertyChanged;

    public bool R1
    {
        get { return r1; }
        set { r1 = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("SubmitEnabled")); }
    }

    public bool R2
    {
        get { return r2; }
        set { r2 = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("SubmitEnabled")); }
    }

    public bool R3
    {
        get { return r3; }
        set { r3 = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("SubmitEnabled")); }
    }

    public bool SubmitEnabled
    {
        get
        {
            return R1 || R2 || R3;
        }
    }
}

你的守则看起来像:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = new ViewModel();
    }
}

答案 1 :(得分:1)

您可能希望处理每个单选按钮的事件处理程序,例如:

<RadioButton GroupName="a" Click="HandleRadioSelection">123</RadioButton>

给你的按钮命名:

<Button Name="SubmitForm" Grid.Row="1" IsEnabled="False">Submit</Button>

然后在你的代码背后:

private void HandleRadioSelection(object sender, RoutedEventArgs e) {
    SubmitForm.IsEnabled = true;
}

答案 2 :(得分:0)

MVVM是基于XAML的应用程序的最佳范例之一。如果OP不想遵循MVVM方法 - 触发器应该做到这一点。

    <StackPanel>
        <RadioButton x:Name="RadioButton1" GroupName="a">123</RadioButton>
        <RadioButton x:Name="RadioButton2" GroupName="a">456</RadioButton>
        <RadioButton x:Name="RadioButton3" GroupName="a">789</RadioButton>
    </StackPanel>

    <Button>
        <Button.Style>
            <Style TargetType="Button">
               <Setter Property="IsEnabled" Value="False"/>
               <Style.Triggers>

                   <DataTrigger Binding="{Binding IsChecked, ElementName=RadioButton1}" Value="True">
                       <Setter Property="IsEnabled" Value="True"/>
                   </DataTrigger>

                    <DataTrigger Binding="{Binding IsChecked, ElementName=RadioButton2}" Value="True">
                        <Setter Property="IsEnabled" Value="True"/>
                    </DataTrigger>

                    <DataTrigger Binding="{Binding IsChecked, ElementName=RadioButton3}" Value="True">
                        <Setter Property="IsEnabled" Value="True"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Button.Style>
    </Button>