2015-03-08 30 views
0

我有一個自定義的RoutedUICommand綁定到按鈕的Command屬性時工作正常。然而,當綁定到列表框的項目上的ConextMenu MenuItem時,它不起作用。 CanExecute方法未被調用,因此該項目在上下文菜單中處於禁用狀態。 CanExecute是從按鈕的綁定中調用的。WPF:命令上的列表框項目上下文菜單/菜單項不起作用,即使它在按鈕上執行

在下面的回購示例中,CrawlCommands.AddCredit是命令。它與一個按鈕和一個ListBoxItems綁定。按鈕的行爲如預期,但ListBoxItem不。右鍵單擊列表框項目不會顯示上下文菜單,並且菜單項的標題從命令中正確地派生出來,但CanExecute從不調用,所以它永遠不會知道該命令是有效的,因此保持禁用狀態。

我錯過了什麼?

<UserControl x:Class="CrawlSpace.TestCommands" 
     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:local="clr-namespace:CrawlSpace" 
     mc:Ignorable="d" 
     d:DesignHeight="300" d:DesignWidth="300" Background="#FF020523"> 


<UserControl.CommandBindings> 
    <CommandBinding Command="local:CrawlCommands.AddCredit" Executed="ExecutedAddCredit" CanExecute="CanExecuteAddCredit"/> 
</UserControl.CommandBindings> 

<Grid> 

    <Grid.Resources> 
     <local:MockData x:Key="TestData"/> 
    </Grid.Resources> 

    <Grid.Background> 
     <SolidColorBrush Color="DarkGray"/> 
    </Grid.Background> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="7*"/> 
     <RowDefinition Height="3*"/> 
    </Grid.RowDefinitions> 

    <!-- A button with a command, this one works! --> 

    <Button x:Name="button" Command="local:CrawlCommands.AddCredit" Content="Add Credit" Margin="10" Grid.Row="1" Width="177"/> 

    <ListBox x:Name="listBox" Margin="10" Background="LightBlue" ItemsSource="{StaticResource TestData}"> 
     <ListBox.Resources> 
      <!-- Define a context menu --> 
      <ContextMenu x:Key="ItemMenu"> 
       <MenuItem Command="local:CrawlCommands.AddCreditBlock"/> 
      </ContextMenu> 

      <!-- Sets a context menu for each ListBoxItem in the current ListBox --> 
      <Style TargetType="{x:Type ListBoxItem}"> 

       <!-- This menu item will not function even though the button verison above does --> 
       <Setter Property="ContextMenu" Value="{StaticResource ItemMenu}"></Setter> 
      </Style> 
     </ListBox.Resources> 

     <ListBox.ItemTemplate> 
      <DataTemplate> 
       <StackPanel Orientation="Horizontal"> 
        <TextBlock Text="{Binding FirstName}"/> 
       </StackPanel> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
    </ListBox> 
</Grid> 

+0

分享細節CrawlCommands。 – 2015-03-08 19:45:19

+0

這只是一個命令,如: \t \t'public static RoutedUICommand AddCredit {get; } = new RoutedUICommand(「Add Credit」,「AddCredit」,typeof(CrawlCommands));' – nsxdavid 2015-03-08 19:47:30

+0

但是在ContextMenu中,它被命名爲AddCreditBlock命令。 Pls chekc' – 2015-03-08 20:06:30

回答

0

請參閱我的代碼。它對我來說工作得很好。

<UserControl x:Class="DragDrop_Learning.UserControl1" 
     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" 
     mc:Ignorable="d" 
     xmlns:local="clr-namespace:DragDrop_Learning" 
     d:DesignHeight="300" d:DesignWidth="300"> 
<UserControl.CommandBindings> 
    <CommandBinding Command="local:CrawlCommands.AddCredit" Executed="ExecutedAddCredit" CanExecute="CanExecuteAddCredit"/> 
    <CommandBinding Command="local:CrawlCommands.AddCreditBlock" Executed="ExecutedAddCreditBlock" CanExecute="CanExecuteAddCreditBlock"/> 
</UserControl.CommandBindings> 

<Grid> 

    <Grid.Resources> 

    </Grid.Resources> 

    <Grid.Background> 
     <SolidColorBrush Color="DarkGray"/> 
    </Grid.Background> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="7*"/> 
     <RowDefinition Height="3*"/> 
    </Grid.RowDefinitions> 

    <!-- A button with a command, this one works! --> 

    <Button x:Name="button" Command="local:CrawlCommands.AddCredit" Content="Add Credit" Margin="10" Grid.Row="1" Width="177"/> 

    <ListBox x:Name="listBox" Margin="10" Background="LightBlue" > 
     <ListBox.Resources> 
      <!-- Define a context menu --> 
      <ContextMenu x:Key="ItemMenu"> 
       <MenuItem Command="local:CrawlCommands.AddCreditBlock"/> 
      </ContextMenu> 

      <!-- Sets a context menu for each ListBoxItem in the current ListBox --> 
      <Style TargetType="{x:Type ListBoxItem}"> 

       <!-- This menu item will not function even though the button verison above does --> 
       <Setter Property="ContextMenu" Value="{StaticResource ItemMenu}"></Setter> 
      </Style> 
     </ListBox.Resources> 

     <ListBox.ItemTemplate> 
      <DataTemplate> 
       <StackPanel Orientation="Horizontal"> 
        <TextBlock Text="{Binding FirstName}"/> 
       </StackPanel> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
    </ListBox> 
</Grid> 

public partial class UserControl1 : UserControl 
{ 
    public UserControl1() 
    { 
     InitializeComponent(); 
     List<Person> lst = new List<Person>(); 
     listBox.ItemsSource = lst; 
     lst.Add(new Person() { FirstName="Test"}); 
    } 

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

    private void ExecutedAddCredit(object sender, ExecutedRoutedEventArgs e) 
    { 

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

    private void ExecutedAddCreditBlock(object sender, ExecutedRoutedEventArgs e) 
    { 

    } 
} 

public static class CrawlCommands 
{ 
    public static readonly RoutedUICommand AddCredit = new RoutedUICommand 
      (
        "AddCredit", 
        "AddCredit", 
        typeof(CrawlCommands) 
      ); 

    public static readonly RoutedUICommand AddCreditBlock = new RoutedUICommand 
      (
        "AddCreditBlock", 
        "AddCreditBlock", 
        typeof(CrawlCommands) 
      ); 
} 

class Person 
{ 
    private string myVar; 

    public string FirstName 
    { 
     get { return myVar; } 
     set { myVar = value; } 
    } 

} 
+0

感謝您的幫助。 :) – nsxdavid 2015-03-09 05:16:13