简体   繁体   中英

WPF C# Command Binding

I have problem with binding, I want to navigate to new Page (not new window). I Have tried by click event, this is working but if I am trying by ICommand interface, it isn't working good. I don't want why, i have tried in cross-platform, everything works fine. I am thinking what's is wrong with that code.

Main XAML

<Window x:Class="WpfAppFP.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:WpfAppFP"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525"
    Background="#67696d"
    x:Name="_CLK">
<Window.Content>
    <Border Background="#202021" CornerRadius="40" >

        <StackPanel Margin="20">
            <Label Content="Type in your data" Foreground="White" HorizontalAlignment="Center" FontSize="20" FontWeight="Bold"/>
            <Separator/>
            <Label Content="Name" HorizontalAlignment="Center" Foreground="White" FontWeight="Bold"/>
            <TextBox HorizontalAlignment="Center" MinWidth="320" MinHeight="32" Foreground="Black" Background="#f5ddff" FontSize="15" FontWeight="DemiBold"/>
            <Label Content="Surname" HorizontalAlignment="Center" Foreground="White" FontWeight="Bold"/>
            <TextBox HorizontalAlignment="Center" MinWidth="320" MinHeight="32" Foreground="Black" Background="#f5ddff" FontSize="15" FontWeight="DemiBold"/>
            <Label Content="Social" HorizontalAlignment="Center" Foreground="White" FontWeight="Bold"/>
            <TextBox HorizontalAlignment="Center" MinWidth="320" MinHeight="32" Foreground="Black" Background="#f5ddff" FontSize="15" FontWeight="DemiBold" MaxLength="11" />
            <Separator Margin="0,10,0,0"/>
            <Button HorizontalAlignment="Center" MinHeight="32" MinWidth="120" Foreground="Black" Background="White" Margin="0,10,0,0" Content="Login" FontWeight="Bold" BorderThickness="2"
                x:Name="BtnClk" Command="{Binding CandidatesVM}"/>

        </StackPanel>
    </Border>
</Window.Content>

MainPage

public partial class MainWindow : Window
{
    CandidatesVM candidatesVM;
    public MainWindow()
    {
        InitializeComponent();
        candidatesVM = new CandidatesVM();
        this.DataContext = candidatesVM;

    }


}

CandidatesVM

 public class CandidatesVM
{
    public NavigationCandidates navigationCandidates { get; set; }


    public CandidatesVM()
    {
        navigationCandidates = new NavigationCandidates(this);
    }

    public void Navigate()
    {
        MainWindow mainWindow = new MainWindow();
        mainWindow._CLK.Content = new Candidates();
    }
}

NavigationCandidates

public class NavigationCandidates : ICommand
{
    public CandidatesVM candidatesVM { get; set; }

    public event EventHandler CanExecuteChanged;

    public NavigationCandidates(CandidatesVM canVM)
    {
        candidatesVM = canVM;
    }

    public bool CanExecute(object parameter)
    {
        return true;
    }

    public void Execute(object parameter)
    {
        candidatesVM.Navigate();
    }
}

Bind to navigationCandidates , that's the command:

<Button x:Name="BtnClk" ... Command="{Binding navigationCandidates}"/>

Then I don't know why you are creating a new instance of the MainWindow in the Navigate() method of CandidatesVM . I guess you want to set the Content of the already existing one:

public class CandidatesVM
{
    public NavigationCandidates navigationCandidates { get; set; }

    private readonly MainWindow _mainWindow;
    public CandidatesVM(MainWindow mainWindow)
    {
        _mainWindow = mainWindow;
        navigationCandidates = new NavigationCandidates(this);
    }

    public void Navigate()
    {
        _mainWindow.Content = new Candidates();
    }
}

public partial class MainWindow : Window
{
    CandidatesVM candidatesVM;
    public MainWindow()
    {
        InitializeComponent();
        candidatesVM = new CandidatesVM(this);
        this.DataContext = candidatesVM;

    }
}

You should be binding to navigationCandidates rather than CandidateVM

 <Button HorizontalAlignment="Center" MinHeight="32" MinWidth="120" Foreground="Black" Background="White" Margin="0,10,0,0" Content="Login" FontWeight="Bold" BorderThickness="2"
                x:Name="BtnClk" Command="{Binding navigationCandidates }"/>

Why ? CandidateVM is served as a data context to your window. So you should be binding to some property of data context and not to data context itself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM