Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi guys.
I have a String variable which works like a Boolean. The data will be "Male" or "Female".
So I Bind RadioButton to it in two way mode.
But I don't know it doesn't work!
Any Help please?

UPDATE:
I changed the IsChecked to Visibility. And it works!!! I mean it changes the visibility!!!
I got confused!

What I have tried:

XML
<RadioButton x:Name="RadioButtonGenderInSubtitleDataGridFEMALE"
             Grid.Row="1"
             Grid.Column="0"
             HorizontalAlignment="Right"
             VerticalAlignment="Stretch"
             HorizontalContentAlignment="Center"
             VerticalContentAlignment="Center"
             Foreground="White" 
             BorderThickness="1"
             Content=""
             ClickMode="Release" 
             IsChecked="False">
    <RadioButton.Style>
        <Style TargetType="RadioButton">
            <Style.Triggers>
                <DataTrigger Binding="{Binding GenderOfCharacter}" Value="Female">
                    <Setter Property="IsChecked" Value="True"/>
                 </DataTrigger>
                 <DataTrigger Binding="{Binding GenderOfCharacter}" Value="Male">
                     <Setter Property="IsChecked" Value="False"/>
                 </DataTrigger>
             </Style.Triggers>
        </Style>
     </RadioButton.Style>
</RadioButton>
Posted
Comments
[no name] 16-Jan-24 17:05pm    
You're treating a RadioButton like a CheckBox. It only has meaning in the context of 2+ RadioButtons.

1 solution

Because it expects a boolean value?

Use an IValueConverter for binding conversions. Here is more information: Value conversion with IValueConverter - The complete WPF tutorial[^]
 
Share this answer
 
Comments
Maciej Los 16-Jan-24 15:04pm    
5ed!
Sh.H. 16-Jan-24 15:25pm    
@Maciej
What do you mean by 5ed! ?
Maciej Los 16-Jan-24 15:33pm    
I voted for that answer: 5 stars (see right-top corner).
Sh.H. 16-Jan-24 15:29pm    
@Graeme_Grant
No it doesn't related to boolean value. I tested with a boolean but no luck.
Also as I mentioned in Update, I accidentally changed the IsChecked to Visibility. And it works!!! I mean when I set to change the Visibility, it works. But when I set IsChecked, it doesn't work.
Graeme_Grant 16-Jan-24 15:48pm    
I did not check the value type. However, unless you're passing the right value type at the binding, you need to use a converter to convert from one value type to another.

I have pointed you to a WPF tutorial site that explains all of this.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900