C# WPF Binding a 2 Radiobuttons as per a Boolean

Asked

Viewed 46 times

2

Good staff.

<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 was predentia was to make a Binding for both, whether the bool was true to mark the top or whether it was false to mark the bottom

To TextBox and the like is easy but now for this situation I am not able to find any information on this.

Thanks

  • A tip, you could use a converter to reverse the value of boolean in the binding according to what you want. From a look here. You would use it and you could also pass a parameter to differentiate each element, and being the radioButtonChange, you would reverse the value of the boolean. To know how to use the ConverterParameter, at a glance here.

1 answer

1


use the datatrigger to where PropriedadeBool is the property Boolean that 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 it by code

public void NomeDoEvento(bool PropriedadeBool){
    radioButtonPlus.isChecked = PropriedadeBool;
    radioButtonChange.isChecked = !PropriedadeBool;
}
  • Thanks for the help :D this shit was already driving me crazy

  • kkk, I’m glad it worked

Browser other questions tagged

You are not signed in. Login or sign up in order to post.