Question x:name not allowing code-behind to access

Hephaestus

New member
Joined
Apr 18, 2018
Messages
1
Programming Experience
1-3
Alright, after searching the internet for a good day I give up.
Here is what I have as XAML:
C#:
<RadioButton x:Name="RadioButton1" Content="Hello" HorizontalAlignment="Left" Margin="225,190,0,0" VerticalAlignment="Top"/>
<RadioButton x:Name="RadioButton2" Content="Goodbye" HorizontalAlignment="Left" Margin="500,190,0,0" VerticalAlignment="Top"/>

However when I try to do this:
C#:
if (RadioButton1.IsChecked == true)            {
                MessageBox.Show("Hello.");
            }
            else
            {
                RadioButton2.IsChecked = true;
                MessageBox.Show("Goodbye.");
            }

I have red squiglies under "RadioButton1" and "RadioButton2"
After changing the x:name multiple times and resetting there name, nothing happens.
Is it maybe because I can't access radiobuttons from the code-behind then main page?
Do I need to be in the code-behind the radiobuttons?
 
I just created a new WPF Application project and added two RadioButtons and a Button. I snapped them in the top-left corner of the window and here's the XAML I got:
C#:
<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <RadioButton Content="RadioButton" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,10,0,0"/>
        <RadioButton Content="RadioButton" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,30,0,0"/>
        <Button Content="Button" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="10,50,0,0"/>

    </Grid>
</Window>
I changed the Name and Content for each RadioButton in the Properties window and here's the XAML I got:
C#:
<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <RadioButton x:Name="RadioButton1" Content="Hello" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,10,0,0"/>
        <RadioButton x:Name="RadioButton2" Content="Goodbye" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,30,0,0"/>
        <Button Content="Button" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="10,50,0,0"/>

    </Grid>
</Window>
I then double-clicked the Button to create a Click event handler and I added the following code:
private void Button_Click(object sender, RoutedEventArgs e)
{
    if (RadioButton1.IsChecked == true)
    {
        MessageBox.Show("Hello");
    }
    else if (RadioButton2.IsChecked == true)
    {
        MessageBox.Show("Goodbye");
    }
}

The application compiled, ran and behaved exactly as expected.
 

Latest posts

Back
Top Bottom