Home > Blockchain >  How to use the click method on a button in WPF? CS8321: The local function is declared but never use
How to use the click method on a button in WPF? CS8321: The local function is declared but never use

Time:12-07

In my XAML code I have the following code:

<Button Name="btnOpenSupplyLine" HorizontalAlignment="Left" Margin="50,75,0,0" VerticalAlignment="Top" Click="OpenSupplyLine" ClickMode="Release" Background="White"  BorderThickness="0">
    <Button.Content>
        <Rectangle Height="200" Stroke="Black"  Width="200" >
        
        </Rectangle>
    </Button.Content>
</Button>

Now when I use this code in my .cs file:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        Console.WriteLine(System.AppDomain.CurrentDomain.BaseDirectory);

        void OpenSupplyLine(object sender, RoutedEventArgs e)
        {
            btnOpenSupplyLine.Background = Brushes.Red;
        }
    }
}

I get the following error message: CS8321: The local function OpenSupplyLine is declared but never used.

What's the proper way of doing a click event on WPF?

CodePudding user response:

As you can see, you wrote the handler inside the constructor, as a local function.

Move it out of the constructor.

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        Console.WriteLine(System.AppDomain.CurrentDomain.BaseDirectory);
    }

    private void OpenSupplyLine(object sender, RoutedEventArgs e)
    {
        btnOpenSupplyLine.Background = Brushes.Red;
    }
}
  • Related