2

I've bound a command to a button . The CanExecute method on the button gets called but clicking on the button doesn't cause the Execute method to be called.

Here is the xaml Code

<Window.CommandBindings>
    <CommandBinding Command="{x:Static local:Invoices.HelloWorldRoutedCommand}"
                    Executed="OnExecute"
                    CanExecute="OnCanExecute"  />
</Window.CommandBindings>
<Window.Resources>
    <DataTemplate x:Key="ItemTemplate" >
        <WrapPanel >
           <Button x:Name="ImageBtn" 
                   Background="Red" 
                Command="{x:Static    local:Invoices.HelloWorldRoutedCommand}">
                <StackPanel >
                    <Image Width="70" Height="55" 
                          Source="{Binding img}"  />
                    <TextBlock Text="{Binding text}"         VerticalAlignment="Center" 
HorizontalAlignment="Center" FontFamily="GE Flow" />
                </StackPanel>

            </Button>
        </WrapPanel>
    </DataTemplate>
</Window.Resources>
<Grid x:Name="Product_grid" Grid.Column="1">
        <ListView  Name="listView" 
          ItemTemplate="{StaticResource ItemTemplate}" >
            <ListView.ItemsPanel>
                <ItemsPanelTemplate>
                    <UniformGrid Columns="5"  />
                </ItemsPanelTemplate>
            </ListView.ItemsPanel>
        </ListView>
        <TextBlock Name="ProductName"
           Text=""
           DataContext="{StaticResource ItemTemplate}" />
</Grid>

Code-behind

public partial class Invoices : Window
{
  public static RoutedCommand HelloWorldRoutedCommand = new RoutedCommand();
  public void OnCanExecute(object sender, CanExecuteRoutedEventArgs e)
   {
     e.CanExecute = true;
   }
  public void OnExecute(object sender, ExecutedRoutedEventArgs e)
   {
      MessageBox.Show("test");
   }
}

0 Answers0