C # WPF Binding to 2 RadioButtons as a boolean

2

Good people.

<GroupBox Header="Tipo de operação" HorizontalAlignment="Left" Height="55" Margin="10,70,0,0" VerticalAlignment="Top" Width="296">
    <Grid x:Name="gridOperationType">
        <RadioButton x:Name="radioButtonPlus" Content="Acumulação" HorizontalAlignment="Left" Margin="0,10,0,0" VerticalAlignment="Top"/>
        <RadioButton x:Name="radioButtonChange" Content="Alteração" HorizontalAlignment="Left" Margin="120,10,0,0" VerticalAlignment="Top"/>
    </Grid>
</GroupBox>

Now what I predicted would be to bind to both, if the bool was true to mark the top or if it was false to mark the bottom

For TextBox and related it is easy but now for this situation I can not find any information about this.

Thanks

    
asked by anonymous 20.09.2018 / 17:55

1 answer

1

use the datatrigger where PropriedadeBool is the boolean property you are checking

<RadioButton x:Name="radioButtonPlus" Content="Acumulação" HorizontalAlignment="Left" Margin="0,10,0,0" VerticalAlignment="Top">
    <RadioButton.Style>
        <Style TargetType="RadioButton">
            <Style.Triggers>
                <DataTrigger Binding="{Binding PropriedadeBool}" Value="True">
                    <Setter Property="IsChecked" Value="True"></Setter>
                </DataTrigger>
                <DataTrigger Binding="{Binding PropriedadeBool}" Value="False">
                    <Setter Property="IsChecked" Value="False"></Setter>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </RadioButton.Style>
</RadioButton>

<RadioButton x:Name="radioButtonChange" Content="Alteração" HorizontalAlignment="Left" Margin="120,10,0,0" VerticalAlignment="Top">
    <RadioButton.Style>
        <Style TargetType="RadioButton">
            <Style.Triggers>
                <DataTrigger Binding="{Binding PropriedadeBool}" Value="True">
                    <Setter Property="IsChecked" Value="False"></Setter>
                </DataTrigger>
                <DataTrigger Binding="{Binding PropriedadeBool}" Value="False">
                    <Setter Property="IsChecked" Value="True"></Setter>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </RadioButton.Style>
</RadioButton>

Now if you want to do code

public void NomeDoEvento(bool PropriedadeBool){
    radioButtonPlus.isChecked = PropriedadeBool;
    radioButtonChange.isChecked = !PropriedadeBool;
}
    
20.09.2018 / 19:15