'How can I handle button Click in XAML?

I am beginner in wpf projects.

i make a wpf project and the code below is my "MainWindow.xaml" file.

<Window x:Class="Test_Xaml.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>
        <Button Content="Add" Click="Button_Click" HorizontalAlignment="Left" Margin="158,127,0,0" VerticalAlignment="Top" Width="75" RenderTransformOrigin="0.507,2">
        </Button>
        <Button Content="Remove" HorizontalAlignment="Left" Margin="158,173,0,0" VerticalAlignment="Top" Width="75" RenderTransformOrigin="-0.027,-0.091">
        </Button>
        <ComboBox HorizontalAlignment="Left" Margin="276,93,0,0" VerticalAlignment="Top" Width="120" RenderTransformOrigin="-0.2,0.286"/>
        <TextBlock HorizontalAlignment="Left" Margin="138,90,0,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Height="24" Width="113"/>
    </Grid>
</Window>

and this is my "MainWindow.xaml.cs" file content.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

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

    }
}

but I receive this error.

Error   1   'Test_Xaml.MainWindow' does not contain a definition for 'Button_Click' and no extension method 'Button_Click' accepting a first argument of type 'Test_Xaml.MainWindow' could be found (are you missing a using directive or an assembly reference?)   C:\Users\Amir\Documents\Visual Studio 2013\Projects\Test Xaml\Test Xaml\MainWindow.xaml 6   108 Test Xaml

please help me I wasted 12 hours to compile my code!



Solution 1:[1]

Check your namespaces. In XAML you have Test_Xaml.MainWindow and in code behind test02

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Posix