CommandParameter始终为NULL

时间:2016-06-24 10:12:42

标签: c# wpf xaml data-binding commandparameter

我想在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>

此Window的DataContext在其Code-behindt中设置为MainWindowViewModel的实例

MainWindowViewModel:

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

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

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

CloseApp中,w始终为空。 Alle其他命令与字符串参数等工作完美 - 我唯一的问题是,我没有得到窗口元素找不到我的viewmodel方式。

感谢您的帮助!

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

1 个答案:

答案 0 :(得分:1)

编辑:在更改应用程序菜单RibbonButton后,您的问题是Microsoft RibbonControlLibrary使用弹出菜单来保存按钮,而MainWindow不是弹出菜单的第一部分(父级) ; s可视树,因此你的ElementName绑定找不到&#34; mainWindow&#34;窗口,所以它为CommandParameter指定null 您可以使用静态Application.Current来获取MainWindow,然后它将起作用 NB! MainWindow是应用程序属性名称,而不是窗口名称

mModelContacts->setFilter(QString("contacts_firstName LIKE '%")+mUi->searchContactsLine->text()+QString("%'"));

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

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

注意CanExecute CanExecute on RelayCommand

的类型和null可能存在的问题