WPF C# 命令绑定

WPF C# Command Binding

我有绑定问题,我想导航到新的 Page(不是新的 window)。我已经通过点击事件尝试过,这是有效的,但如果我通过 ICommand 界面尝试,则效果不佳。我不想为什么,我已经尝试过跨平台,一切正常。我在想那个代码有什么问题。

主要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>

主页

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

    }


}

候选人VM

 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();
    }
}

您应该绑定到 navigationCandidates 而不是 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 }"/>

为什么? CandidateVM 用作您的 window 的数据上下文。因此,您应该绑定到某些 属性 数据上下文,而不是数据上下文本身。

绑定到navigationCandidates,就是命令:

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

那我不知道你为什么要在 CandidatesVMNavigate() 方法中创建一个新的 MainWindow 实例。我想你想设置已经存在的Content

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;

    }
}