0
votes

I am having a problem getting and event handlers to work for a button. I am using Visual Studio 2015. My code and error is below:

XAML:

<Window x:Class="WpfApplication1.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:WpfApplication1" mc:Ignorable="d" Title="MainWindow" Height="500" Width="983.334">
.
.
.
    <Button x:Name="Button1" Content="Database" HorizontalAlignment="Left" 
            Margin="10,427,0,0" VerticalAlignment="Top" Width="99" 
            Click="Button1_Click"/>

Code behind:

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void Button1_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("Test");
        }
    }
}

Error: CS1061 'MainWindow' does not contain a definition for 'Button1_Click' and no extension method 'Button1_Click' accepting a first argument of type 'MainWindow' could be found (are you missing a using directive or an assembly reference?)

Whenever I add a button and click on the event handler section this is what I see: The document item had no code-behind file. Add a code-behind file and a class definition before adding event handlers.

Any help would be appreciated.

2
Can you post the opening <Window> element and attributes from your XAML file? - MickyD
<Window x:Class="WpfApplication1.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:WpfApplication1" mc:Ignorable="d" Title="MainWindow" Height="500" Width="983.334"> - CausedByMonkey
Thanks, generally whack that into your question by editing it. I've done it for you :) - MickyD
Sorry fairly new here. Learning as I go. Thanks for the help. - CausedByMonkey
Not a problem good sir :) - MickyD

2 Answers

0
votes

It works for me for the following XAML

<Window x:Class="WpfApplication1.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:WpfApplication1" mc:Ignorable="d" Title="MainWindow" Height="500" Width="983.334">
    <Button x:Name="Button1" Content="Database" HorizontalAlignment="Left" 
        Margin="10,427,0,0" VerticalAlignment="Top" Width="99" Click="Button1_Click"/>
</Window>

and the following code behind file

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button1_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("Test");
        }
    }

which is exactly what you have. Have you tried rebuilding the solution?

0
votes

Sound like the Event isn't registered in the page, click on your button and check the events tab to see if it's registered... check this image: https://postimg.org/image/5q6nvac7b/