0

- the red markups are for Inputs(xAxis and yAxis) - after i press the button (Calculate), the inputs should be calculated. - then outputed at the blue markup(texbox called Output)

After i made some kata with element binding, i just wanted to start MVVM property binding.

For some reason i get a ErrorCode: CS1061

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

the weird part of this is when i use the resharper eventhandler on my MainWindow.xml at my button it creates a event in my MainWindow.cs. But it didnt before. the events were autocreated in my ViewModel.cs

im not sure what causes this error ty in advance when somone could help me, i have already been sitting on this kata for more then 8 hours.

Heres my MainWindow.xml:

<Window x:Class="Coordinates.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:Coordinates"
    mc:Ignorable="d"
    Title="MainWindow" Height="250" Width="525">
<Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Label Grid.Row="0" Grid.Column="0" Content="Insert X-Coordiante"/>
        <Label Grid.Row="0" Grid.Column="1" Content="Insert Y-Coordinate"/>
        <TextBox Grid.Row="1" Grid.Column="0" Name="TxtXaxis" Text="{Binding Xaxis}"/>
        <TextBox Grid.Row="1" Grid.Column="1" Name="TxtYaxis" Text="{Binding Xaxis}"/>
        <TextBox Grid.Row="2" Grid.Column="0" Text="{Binding Output}"/>
        <Button  Grid.Row="2" Grid.Column="1" Name="Calculate" Click="CalculateClick">Calculate</Button>

</Grid>

This is my MainWinow.xml.cs:

    using System.Windows;
    namespace Coordinates
    {
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.InitializeComponent();
            this.DataContext = new ViewModel();
        }
    }
}

This is my ViewModel.cs:

using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows;

namespace Coordinates
{
    public class ViewModel : INotifyPropertyChanged
    {
        #region Inputs
        private double _xaxis;
        public double Xaxis
        {
            get => this._xaxis;
            set
            {
                if (value == this._xaxis)
                {
                    return;
                }
                this._xaxis = value;
                this.OnPropertyChanged();
            }
        }

        private double _yaxis;
        public double Yaxis
        {
            get => this._yaxis;
            set
            {
                if (value == this._yaxis)
                {
                    return;
                }
                this._yaxis = value;
                this.OnPropertyChanged();
            }
        }
        #endregion

        public void CalculateClick(object sender, RoutedEventArgs e)
        {
            Output = Math.Sqrt(Math.Pow(Xaxis,2)+Math.Pow(Yaxis,2));
        }

        private double _output;
        public double Output
        {
            get => this._output;
            set
            {
                if (value == this._output)
                {
                    return;
                }
                this._output = value;
                this.OnPropertyChanged();
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private  void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

    }
}

EDIT: this was missing in CalculateClick in my viewmodel: Output = Math.Sqrt(Math.Pow(Xaxis,2)+Math.Pow(Yaxis,2));

  • It is searching for the `CalculateClick` in the `MainWindow`. To bind buttons to ViewModels see https://stackoverflow.com/questions/12422945/how-to-bind-wpf-button-to-a-command-in-viewmodelbase – Gabriel Prá Feb 13 '18 at 15:02
  • The name of the button should be CalculateClick, and don't set the click handler. What mvvm framework are you using? – Ron Beyer Feb 13 '18 at 15:02
  • im using the resharper and the PropertyChangedAnalyzers Packet –  Feb 14 '18 at 12:56

1 Answers1

0

You are using a CalculateClick as Eventhandler for the Click Event of the Button. Per Default, WPF looks in the Code Behind (MainWindow.cs) for the Eventhandler, which is not there.

Since you are already initializing your DataContext within the Code Behind. Make your ViewModel a private member of your MainWindow.

Create an event handler in the MainWindow Code Behind and call your event handler in the ViewModel from there.

public partial class MainWindow : Window
{
    private _viewModel = new ViewModel();
    public MainWindow()
    {
        InitializeComponent();
        this.InitializeComponent();
        this.DataContext = _viewModel;
    }
}

public void CalculateClick(object sender, RoutedEventArgs e)
{
    _viewModel.CalculateClick(Sender, e);
}

You might also want to look into Commands, which can be created directly inside of the ViewModel and then be bound to. There is an ActionCommand or GenericCommand, which take an Action or Action<T> and are pretty easy to use.

Andreas
  • 828
  • 4
  • 15
  • that did not work actually but ty for telling me about ActionCommand and GenericComand –  Feb 14 '18 at 12:57