如何将验证绑定到RadioButton WPF

时间:2019-05-28 13:56:49

标签: c# wpf validation xaml

单选按钮绑定显示验证时出现问题。

我创建了两个RadioButtons

<StackPanel Grid.Column="2" Grid.Row="1" Orientation="Horizontal" VerticalAlignment="Top" Margin="0,13,0,0">
    <RadioButton IsChecked="{Binding EndUser, Mode=TwoWay}" Content="End User" />
    <RadioButton IsChecked="{Binding AppDeveloper, Mode=TwoWay}" Margin="15,0,0,0" Content="App Developer"/>
</StackPanel>

根据我的逻辑,我应该从2中取1个名字。

我写了逻辑

[Required]
    public string Role
    {
        get => role;
        set
        {
            Set(ref role, value);
            RaisePropertyChanged("EndUser");
            RaisePropertyChanged("AppDeveloper");
        }
    }

    public bool EndUser
    {
        get => Role.Contains("EndUser");
        set => Role = "EndUser";
    }

    public bool AppDeveloper
    {
        get => Role.Contains("AppDeveloper");
        set => Role = "AppDeveloper";
    }

问题是如何在表单中显示[Required],如果我选择其中的1个,则验证为true(如果需要验证代码,验证工作就可以了)

我找到了这个Validation Rule for Radio buttons wpf,但是这个示例对我不起作用,打破了所有逻辑(什么都没给我发送)并且没有标记我的领域。

如何编写

 <Binding.ValidationRules>
     <DataErrorValidationRule />
 </Binding.ValidationRules>

对于诸如TextBox中的单选按钮字段并将其标记为红色?

我的按钮,如果字段无效,该按钮将禁用

<Button x:Name="SignInButton" Command="{Binding SignInCommand}" Grid.Row="4" Grid.Column="1" Grid.ColumnSpan="2"  Content="Sign In" >
        <Button.Style>
            <Style TargetType="{x:Type Button}">
                <Setter Property="Background" Value="MidnightBlue"/>
                <Setter Property="Foreground" Value="White"/>
                <Setter Property="FontSize" Value="16"/>
                <Setter Property="FontWeight" Value="Bold"/>
                <Setter Property="Width" Value="100"/>
                <Setter Property="Height" Value="40"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type Button}">
                            <Border Background="{TemplateBinding Background}" CornerRadius="10">
                                <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" Height="23" Margin="24,5,24,4"/>
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
                <Setter Property="IsEnabled" Value="False"/>
                <Style.Triggers>
                    <MultiDataTrigger>
                        <MultiDataTrigger.Conditions>
                            <Condition Binding="{Binding Path=(Validation.HasError), ElementName=Password}" Value="False"/>
                            <Condition Binding="{Binding Path=(Validation.HasError), ElementName=Login}" Value="False"/>
                            <Condition Binding="{Binding Path=(Validation.HasError), ElementName=Role}" Value="False"/>
                        </MultiDataTrigger.Conditions>
                        <Setter Property="IsEnabled" Value="True"/>
                    </MultiDataTrigger>

                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Background" Value="#FF280895"/>
                    </Trigger>
                </Style.Triggers>
            </Style>
        </Button.Style>
    </Button>

MultiDataTrigger中,如果按钮的字段为Validation.HasError,则我写了规则

1 个答案:

答案 0 :(得分:0)

例如,您可以将Border放在RadioButtons周围,并使用Style和绑定到DataTrigger属性的Role

<Border Grid.Column="2" Grid.Row="1" BorderBrush="Red" Margin="0,13,0,0">
    <StackPanel Orientation="Horizontal" VerticalAlignment="Top">
        <RadioButton IsChecked="{Binding EndUser, Mode=TwoWay}" Content="End User" />
        <RadioButton IsChecked="{Binding AppDeveloper, Mode=TwoWay}" Margin="15,0,0,0" Content="App Developer"/>
    </StackPanel>
    <Border.Style>
        <Style TargetType="Border">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Role}" Value="{x:Null}">
                    <Setter Property="BorderThickness" Value="1" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Border.Style>
</Border>

然后,您应将角色的设置者设为私有,并确保引发所涉及属性的PropertyChanged事件:

private string role;
[Required]
public string Role
{
    get => role;
    private set
    {
        role = value;
        RaisePropertyChanged("Role");
    }
}

public bool EndUser
{
    get => Role == "EndUser";
    set
    {
        Role = "EndUser";
        RaisePropertyChanged("EndUser");
        RaisePropertyChanged("AppDeveloper");
    }
}

public bool AppDeveloper
{
    get => Role == "AppDeveloper";
    set
    {
        Role = "AppDeveloper";
        RaisePropertyChanged("AppDeveloper");
        RaisePropertyChanged("EndUser");
    }
}