Results 1 to 2 of 2

Thread: x:name not allowing code-behind to access

  1. #1
    .NET Framework
    .NET 4.5
    Join Date
    Apr 2018
    Posts
    1
    Rep Power
    0

    Question x:name not allowing code-behind to access

    Alright, after searching the internet for a good day I give up.
    Here is what I have as XAML:
    Code:
    <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:
    Code:
    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?

  2. #2
    .NET Framework
    .NET 4.5
    Join Date
    Apr 2011
    Posts
    1,886
    Rep Power
    146
    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:
    Code:
    <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:
    Code:
    <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.

Similar Threads

  1. Question Random file access
    By pumper in forum C# General Discussion
    Replies: 17
    Last Post: 05-16-2016, 10:23 AM
  2. exit console application code, also center code?
    By jacob_1988 in forum Console Application
    Replies: 1
    Last Post: 03-26-2016, 3:55 AM
  3. Question The best way to INSERT to Access ?
    By TheCoderMoe in forum MS Access
    Replies: 4
    Last Post: 02-22-2014, 3:55 PM
  4. Question System.Windows is not allowing me to use Window
    By unSatisfied in forum C# General Discussion
    Replies: 1
    Last Post: 01-25-2014, 11:23 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •