WPF:如何使命令? [英] WPF: How to enable a Command?

查看:84
本文介绍了WPF:如何使命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道为什么在我的上下文菜单中添加项目时,才会启用时,有在ListView选择一个项目。有谁知道这是为什么。



下面是我的XAML代码



 <?;窗口x:类=Vokabular1.MainWindow
的xmlns =htt​​p://schemas.microsoft.com/winfx/2006/xaml/presentation
的xmlns:X =HTTP://模式。 microsoft.com/winfx/2006/xaml
标题=主窗口HEIGHT =350WIDTH =525>
<网格和GT;
<电网的Horizo​​ntalAlignment =拉伸NAME =网格VerticalAlignment =拉伸>
< Grid.ColumnDefinitions>
< ColumnDefinition />
< ColumnDefinition />
< /Grid.ColumnDefinitions>

< ListView控件Grid.Column =0的Horizo​​ntalAlignment =拉伸保证金=10,10,10,10NAME =ListView控件VerticalAlignment =拉伸>
< ListView.View>
< GridView控件/>
< /ListView.View>
< ListView.CommandBindings>
<命令的CommandBinding =新
所执行=CommandBinding_Executed
CanExecute =CommandBinding_CanExecute/>
< /ListView.CommandBindings>
< ListView.ContextMenu>
<&文本菜单GT;
<菜单项名称=添加标题=_ add命令=新建/>
<菜单项标题=删除命令=删除IsEnabled =真/>
< /文本菜单>
< /ListView.ContextMenu>
< ListViewItem的/>
< /&的ListView GT;
< /网格和GT;
< /网格和GT;
< /窗GT;



为窗口的方法是:



 私人无效CommandBinding_Executed(对象发件人,ExecutedRoutedEventArgs E)
{
MessageBox.Show(OK);
}

私人无效CommandBinding_CanExecute(对象发件人,CanExecuteRoutedEventArgs E)
{
e.CanExecute = TRUE;
e.Handled = TRUE;
}


解决方案

是必需的焦点 CommandBinding.CanExecute 被调用。由于在的ListView 选择项目强制焦点转移到的ListView ; ,可能会出现评估



如果你放置 listView.Focus(); 中的窗口的构造函数,你会注意到 CommandBinding.CanExecute 如预期现在被称为,因此没有一个项目被启用或包含在<$选择C $ C>的ListView 。



移动绑定到窗口仍需要重点要获得窗口中设置;无论是通过构造函数内部或通过其他方法的显式调用;恩......在在的ListView 或其它控制选择项目窗口可接收焦点。


I don't know why the Add item in my context menu is only enabled when there is an item selected in the ListView. Does anybody knows why?

Here's my XAML code

    <Window x:Class="Vokabular1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid HorizontalAlignment="Stretch" Name="grid" VerticalAlignment="Stretch">
            <Grid.ColumnDefinitions>
                <ColumnDefinition />
                <ColumnDefinition />
            </Grid.ColumnDefinitions>

            <ListView Grid.Column="0" HorizontalAlignment="Stretch" Margin="10,10,10,10" Name="listView" VerticalAlignment="Stretch">
                <ListView.View>
                    <GridView />
                </ListView.View>
                <ListView.CommandBindings>
                    <CommandBinding Command="New" 
                        Executed="CommandBinding_Executed" 
                        CanExecute="CommandBinding_CanExecute" />
                </ListView.CommandBindings>
                <ListView.ContextMenu>
                    <ContextMenu>
                        <MenuItem Name="Add" Header="_Add"    Command="New" />
                        <MenuItem Header="Delete" Command="Delete" IsEnabled="True" />
                    </ContextMenu>
                </ListView.ContextMenu>
                <ListViewItem />  
            </ListView>            
        </Grid>
    </Grid>
</Window>

The methods for the window are:

private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
{
    MessageBox.Show("ok");
}

private void CommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
    e.CanExecute = true;
    e.Handled = true;
}

解决方案

Focus is required for the CommandBinding.CanExecute to be called. Since selecting an item in the ListView forces the focus to shift to the ListView; the evaluation can occur.

If you were to place listView.Focus(); within your Window constructor you would note that CommandBinding.CanExecute is now called as expected and therefore enabled without an item being contained or selected within the ListView.

Moving the binding to the Window will still require the focus to get set within the Window; either via an explicit call within the constructor or by another means; ex... selecting an item in the ListView or other control within the Window which can receive focus.

这篇关于WPF:如何使命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆