簡體   English   中英

如何在用戶控件中使用命令

[英]How do I use a command in a user control

我試圖使用我創建的命令。 我正在為我的應用程序使用c#和MVVM結構。 我有1個窗口(ApplicationView),用於顯示我的userContols(MainWindow,Window1)。 在啟動時,我會顯示MainWindow用戶控件,並且該用戶控件上有一個按鈕,當您按下該按鈕時,會將用戶控制從主窗口usercontol更改為Window1 userContol。

ApplicationView.xaml(窗口)

<Window x:Class="Nmenq.ApplicationView"
    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:Nmenq"
    xmlns:my="clr-namespace:Nmenq"
    Title="ApplicationView" Height="400" Width="575">

<Window.Resources>
    <DataTemplate DataType="{x:Type local:MainWindowViewModel}">
        <local:MainWindow/>
    </DataTemplate>
    <DataTemplate DataType="{x:Type local:Window1ViewModel}">
        <local:Window1/>
    </DataTemplate>
</Window.Resources>
<DockPanel LastChildFill="True">
    <ContentControl x:Name="Pages" DockPanel.Dock="Right" Content="{Binding CurrentPageViewModel}"/>
</DockPanel>

ApplicationView.xaml.cs

public partial class ApplicationView : Window
{
    public ApplicationView()
    {
        InitializeComponent();
        this.DataContext = new NavigationViewModel();
    }
}

MainWindow.xaml(UserContol)

<UserControl x:Class="Nmenq.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:Nmenq"
    xmlns:viewmodel="clr-namespace:Nmenq.ViewModel"
    xmlns:my="clr-namespace:Nmenq"
    mc:Ignorable="d"
    d:DesignHeight="400" d:DesignWidth="575">


<Grid DataContext="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}">
    <Button x:Name="Accept_Button" Content="Accept" HorizontalAlignment="Left" Margin="475,316,0,0" VerticalAlignment="Top" Width="81" Command="{Binding Window1Command}"/>   
</Grid>

MainWindow.xmal.cs

    public partial class MainWindow : UserControl, INotifyPropertyChanged
{

    NavigationViewModel object1 = new NavigationViewModel();

    public ICommand Window1Command { get; set; }

    public MainWindow()
    {
        InitializeComponent();
        Window1Command = object1.Window1Command;
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public void OnPropertyChanged(string name)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(name));
        }
    }
}

NavigationViewModel(在此處設置我的命令)

    class NavigationViewModel : INotifyPropertyChanged
{
    public ICommand MainWindowCommand { get; set; }
    public ICommand Window1Command { get; set; }

    private object currentPageViewModel;
    public object CurrentPageViewModel
    {
        get { return currentPageViewModel; }
        set { currentPageViewModel = value; OnPropertyChanged("CurrentPageViewModel"); }
    }

    public NavigationViewModel()
    {
        MainWindowCommand = new BaseCommand(OpenMainWindow);
        Window1Command = new BaseCommand(OpenWindow1);

        CurrentPageViewModel = new MainWindowViewModel();

    }

    public void OpenMainWindow(object obj)
    {
        CurrentPageViewModel = new MainWindowViewModel();
    }

    public void OpenWindow1(object obj)
    {
        CurrentPageViewModel = new Window1ViewModel();

    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(string propName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propName));
        }
    }
}

public class BaseCommand : ICommand
{
    private Predicate<object> _canExecute;
    private Action<object> _method;
    public event EventHandler CanExecuteChanged;

    public BaseCommand(Action<object> method)
        : this(method, null)
    {
    }

    public BaseCommand(Action<object> method, Predicate<object> canExecute)
    {
        _method = method;
        _canExecute = canExecute;
    }

    public bool CanExecute(object parameter)
    {
        if (_canExecute == null)
        {
            return true;
        }

        return _canExecute(parameter);
    }

    public void Execute(object parameter)
    {
        _method.Invoke(parameter);
    }
}    class NavigationViewModel : INotifyPropertyChanged
{
    public ICommand MainWindowCommand { get; set; }
    public ICommand Window1Command { get; set; }

    private object currentPageViewModel;
    public object CurrentPageViewModel
    {
        get { return currentPageViewModel; }
        set { currentPageViewModel = value; OnPropertyChanged("CurrentPageViewModel"); }
    }

    public NavigationViewModel()
    {
        Window1Command = new BaseCommand(OpenWindow1);

        CurrentPageViewModel = new MainWindowViewModel();

    }

    public void OpenWindow1(object obj)
    {
        CurrentPageViewModel = new Window1ViewModel();

    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(string propName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propName));
        }
    }
}

public class BaseCommand : ICommand
{
    private Predicate<object> _canExecute;
    private Action<object> _method;
    public event EventHandler CanExecuteChanged;

    public BaseCommand(Action<object> method)
        : this(method, null)
    {
    }

    public BaseCommand(Action<object> method, Predicate<object> canExecute)
    {
        _method = method;
        _canExecute = canExecute;
    }

    public bool CanExecute(object parameter)
    {
        if (_canExecute == null)
        {
            return true;
        }

        return _canExecute(parameter);
    }

    public void Execute(object parameter)
    {
        _method.Invoke(parameter);
    }
}

任何幫助或建議,將不勝感激:)

您可以直接綁定到NavigationViewModelWindow1Command屬性,並從UserControl刪除Window1Command屬性:

<Button x:Name="Accept_Button" Content="Accept" HorizontalAlignment="Left" Margin="475,316,0,0" VerticalAlignment="Top" Width="81" 
        Command="{Binding DataContext.Window1Command, RelativeSource={RelativeSource AncestorType=Window}}"/>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM