I have a ListBox the ItemTemplate bound to an ObservableCollection of my items. At the moment, I'm trying to implement Cut/Copy/Paste/SelectAll (To keep it short, I'll just show selectall here...)
<UserControl.CommandBindings>
<CommandBinding Command="SelectAll" CanExecute="SelectAll_CanExecute" Executed="SelectAll_Executed"/>
</UserControl.CommandBindings>
<ListBox x:Name="listbox"
ItemsSource="{Binding}"
Background="Transparent"
SelectionMode="Extended"
ScrollViewer.VerticalScrollBarVisibility="Auto">
<ListBox.ContextMenu>
<ContextMenu>
<MenuItem Command="SelectAll" />
</ContextMenu>
</ListBox.ContextMenu>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Background="Transparent">
<CheckBox Name="cbEnabled" IsChecked="{Binding Enabled, Mode=TwoWay}" Margin="0,2,0,0"/>
<TextBlock Text="{Binding Name}" Padding="5,0,0,0"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
this is the codebehind for canexecute:
private void SelectAll_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = listbox.Items.Count > 0;
e.Handled = true;
}
when I first run the program and right-click in the listbox, the "Select All" context menu is always disabled (and SelectAll_CanExecute is never called) until I select something. Is there any way to get this to work like it seems it should? (and without either auto-selecting the first item or making the user have to do it)
Thanks!