Zaz
Zaz

Reputation: 1102

How to start a new WPF page by pressing a button

I'M having a problem with start a new WPF page with use of commands. I tried with use a new WPF window by writing but nothing happens.

I can't see the error? And the program works fine but when the button is pressed, nothing happens

My XAML.

<Button 
            Command="{Binding Path=OpenCrudCommand, UpdateSourceTrigger=PropertyChanged}" 
            Content="CRUD" 
            HorizontalAlignment="Left" 
            Margin="10,352,0,0" 
            VerticalAlignment="Top" 
            Width="83"/>

My OpenCrudCommand.

As you can see, I have tried with a new WPF window, not a WPF page and it didn't work either.

Page 1 is a WPF window form and Page 2 is a WPF page form

{
    class OpenCrudCommand
    {
        ProductViewModel _avm;

         public OpenCrudCommand(ProductViewModel avm)
        {
            _avm = avm;
        }



        public void Execute(object parameter)
        {
            var hej = new Page2();
            hej.Show();
        }

    }
}

I have another question, should i write the code for opening a new page in command or in the viewmodel?

Upvotes: 2

Views: 450

Answers (1)

Backlash
Backlash

Reputation: 541

For more clarity, you're binding to a generic (POCO) class. Typically you must bind to a class that implements the ICommand interface. Do a search for RelayCommand or DelegateCommand to see several implementations. Now once that is done, you will set up a class (typically a ViewModel class) that will serve as the DataContext for your WPF window. Then you will expose a property on your ViewModel that exposes the command (i.e.)

public ICommand MyCommand
{
     get
     {
          return this.myCommand;
     }
}

Then your binding will be along the lines of Command="{Binding MyCommand}" (You do not need the UpdateSourceTrigger property).

If this is still confusing, feel free to follow up with additional questions, but I would suggest reading more about the MVVM pattern.

Upvotes: 2

Related Questions