Reputation: 7164
I'm trying to create a very simple C# program.
Here is the xaml file :
<Window x:Class="HelloWPFApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TextBlock HorizontalAlignment="Left" Margin="104,72,0,0" TextWrapping="Wrap" Text="Select a message option and then choose the Display button" VerticalAlignment="Top"/>
<RadioButton x:Name="RadioButton1" Content="Hello;" HorizontalAlignment="Left" Margin="104,138,0,0" VerticalAlignment="Top" Checked="RadioButton1_Checked"/>
<RadioButton x:Name="RadioButton2" Content="Goodbye;" HorizontalAlignment="Left" Margin="342,138,0,0" VerticalAlignment="Top"/>
<Button Content="Display" HorizontalAlignment="Left" Margin="211,208,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
</Grid>
</Window>
Here is the .cs file :
using....
namespace HelloWPFApp
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
if (RadioButton1.IsChecked == true)
MessageBox.Show("Hello");
else
MessageBox.Show("Goodbye");
}
}
}
And this is the error I get :
'HelloWPFApp.MainWindow' does not contain a definition for 'RadioButton1_Checked' and no extension method 'RadioButton1_Checked' accepting a first argument of type 'HelloWPFApp.MainWindow' could be found (are you missing a using directive or an assembly reference?)
Can you tell me what the problem is?
Upvotes: 5
Views: 19195
Reputation: 354
define a method for the event radio button checked, add this to MainWindow.xaml.cs
void RadioButton1_Checked(object sender, RoutedEventArgs e)
{
//add your job here
}
or replace this line
<RadioButton x:Name="RadioButton1" Content="Hello;" HorizontalAlignment="Left" Margin="104,138,0,0" VerticalAlignment="Top" Checked="RadioButton1_Checked"/>
by this one
<RadioButton x:Name="RadioButton1" Content="Hello;" HorizontalAlignment="Left" Margin="104,138,0,0" VerticalAlignment="Top"/>
Upvotes: 3
Reputation: 1697
You have defined an event Handler in your xaml code
<RadioButton x:Name="RadioButton1" Content="Hello;" HorizontalAlignment="Left" Margin="104,138,0,0" VerticalAlignment="Top" Checked="RadioButton1_Checked"/>
But you have not defined the same in your code-behind. That's what causing the problem. Either remove the Checked Event from the above radio button or define an event handler in your code behind.
Upvotes: 9