簡體   English   中英

如何將驗證綁定到RadioButton WPF

[英]How bind validation to RadioButton WPF

我對Radiobutton綁定有一些問題,以顯示驗證。

我創建了兩個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] ,如果我選擇其中一個,驗證將為真(驗證工作正確,如果需要我顯示驗證代碼)

我找到了這個單選按鈕驗證規則wpf ,但這個例子對我不起作用,打破了所有邏輯(不發送給我任何東西)並且沒有標記我的字段。

怎么寫的

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

對於像TextBox這樣的radiobutton字段並將其標記為紅色?

我的按鈕,如果字段無效則禁用

<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按鈕被禁用

例如,您可以在RadioButtons周圍放置一個Border ,並使用一個Style與一個綁定到Role屬性的DataTrigger

<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>

然后,您應該將Role的setter設為private,並確保為涉及的屬性引發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");
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM