2

我想在 WPF 窗口中实现一个简单的关闭按钮。窗口基本上看起来像这样:

    <Window x:Class="MyApplication.MainWindow"
            xmlns=""....
            ...."
            Title="MainWindow" WindowState="Maximized" WindowStartupLocation="CenterScreen" x:Name="mainWindow">
<DockPanel LastChildFill="True">
        <Ribbon DockPanel.Dock="Top">
            <Ribbon.ApplicationMenu>
                <RibbonApplicationMenu SmallImageSource="Resources/menu_16x16.png">
                    <RibbonApplicationMenu.FooterPaneContent>
                        <RibbonButton Label="Beenden" 
                                      Command="{Binding CmdCloseApp}" 
                                      CommandParameter="{Binding ElementName=mainWindow}"
                                      SmallImageSource="Resources/ende_16x16.png" 
                                      HorizontalAlignment="Right"/>
                    </RibbonApplicationMenu.FooterPaneContent>
                </RibbonApplicationMenu>
            </Ribbon.ApplicationMenu>
        </Ribbon>
</DockPanel>
    </Window>

此窗口的 DataContext 在其代码隐藏中设置为MainWindowViewModel

主窗口视图模型:

public class MainWindowViewModel
{
        public ICommand CmdCloseApp { get; set; }

        public MainWindowViewModel()
        {
            CmdCloseApp = new RelayCommand<Window>(CloseApp);
        }

        public void CloseApp(Window w)
        {
            w.Close();
        }
}

CloseAppw 中始终为空。所有其他带有字符串参数等的命令都可以完美运行 - 我唯一的问题是我没有得到窗口元素,也没有找到通往我的视图模型的方法。

感谢您的帮助!

编辑 我很抱歉,我用一个简单的按钮尝试了它并且它有效 - 问题只发生在RibbonButton

4

1 回答 1

3

编辑:更改应用程序菜单 RibbonButton 后,您的问题是 Microsoft RibbonControlLibrary 使用弹出菜单来保存按钮,而 MainWindow 不是弹出菜单可视树的一部分(父级),因此您的 ElementName 绑定找不到“ mainWindow" 窗口,因此它将 null 分配给 CommandParameter
您可以使用静态 Application.Current 来获取 MainWindow,然后它将起作用。
注意!MainWindow 是应用程序属性名称,而不是您的窗口的名称

<Window x:Class="WpfRelayCommandParameter.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:wpfRelayCommandParameter="clr-namespace:WpfRelayCommandParameter"
    xmlns:ribbon="http://schemas.microsoft.com/winfx/2006/xaml/presentation/ribbon"
    mc:Ignorable="d"
    d:DataContext="{d:DesignInstance wpfRelayCommandParameter:MainWindowViewModel}"
    Title="MainWindow" Height="350" Width="525"
    x:Name="mainWindow">
<DockPanel LastChildFill="True">
    <DockPanel LastChildFill="True">
        <ribbon:Ribbon DockPanel.Dock="Top">
            <ribbon:Ribbon.ApplicationMenu>
                <ribbon:RibbonApplicationMenu SmallImageSource="Resources/AppMenu.png">
                    <ribbon:RibbonApplicationMenu.FooterPaneContent>
                        <ribbon:RibbonButton Label="Beenden" 
                                  Command="{Binding CmdCloseApp}" 
                                  CommandParameter="{Binding MainWindow, Source={x:Static Application.Current}}"
                                  SmallImageSource="Resources/Exit.png" 
                                  HorizontalAlignment="Right" Click="ButtonBase_OnClick"/>
                    </ribbon:RibbonApplicationMenu.FooterPaneContent>
                </ribbon:RibbonApplicationMenu>
            </ribbon:Ribbon.ApplicationMenu>
        </ribbon:Ribbon>
    </DockPanel>
</DockPanel>

就我个人而言,我更喜欢窗口的回调来通过一个可以根据您的视图模型需求自定义的接口,并且可以在单元测试中进行模拟:

<Window x:Class="WpfRelayCommandParameter.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:wpfRelayCommandParameter="clr-namespace:WpfRelayCommandParameter"
    mc:Ignorable="d"
    d:DataContext="{d:DesignInstance wpfRelayCommandParameter:MainWindowViewModel}"
    Title="MainWindow" Height="350" Width="525"
    x:Name="mainWindow">
<Grid>
    <Button Content="Close Window" 
            Command="{Binding CmdCloseApp}" 
            VerticalAlignment="Top"
            HorizontalAlignment="Left" />
</Grid>

代码

public partial class MainWindow : Window, IMainWindow
{
    public MainWindow()
    {
        DataContext = new MainWindowViewModel(this);
        InitializeComponent();
    }
}

public interface IMainWindow
{
    void Close();
}

public class MainWindowViewModel
{
    private readonly IMainWindow _mainWindow;

    public MainWindowViewModel() : this(null)
    {
        // Design time
    }

    public MainWindowViewModel(IMainWindow mainWindow)
    {
        _mainWindow = mainWindow;
        CmdCloseApp = new RelayCommand(CloseApp);
    }

    public ICommand CmdCloseApp { get; set; }

    public void CloseApp(object parameter)
    {
        _mainWindow.Close();
    }
}

注意RelayCommand 上 CanExecute CanExecute的 type 和 null 可能存在的问题

于 2016-06-24T13:11:19.923 回答