我不知道为什么只有在ListView中选择了一个项目时才启用我的上下文菜单中的添加项目.有人知道为什么吗?
这是我的XAML代码
<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>
Run Code Online (Sandbox Code Playgroud)
窗口的方法是:
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;
}
Run Code Online (Sandbox Code Playgroud)
需要关注CommandBinding.CanExecute以进行调用.由于在ListView力量中选择一个项目,焦点转移到ListView; 评估可以发生.
如果你要放在listView.Focus();你的Window构造函数中,你会注意到CommandBinding.CanExecute现在按预期调用它,因此在没有包含或选择项目的情况下启用ListView.
将绑定移动到遗骸Window仍然需要将焦点设置在Window; 通过构造函数中的显式调用或通过其他方式; ex ...选择可以获得焦点的ListView其他控件中的Window项目.
| 归档时间: |
|
| 查看次数: |
1767 次 |
| 最近记录: |