WPF - EventSetter

WPF中添加Event

1. ListBox中添加Event

<ListBox x:Name="itemsControl"  BorderThickness="0" ContextMenuService.IsEnabled="{Binding IsContextMenuOpen,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}"
                                                                                     ScrollViewer.HorizontalScrollBarVisibility="Disabled" 
                                                                                     Focusable="True"
                                                                                     ItemsSource="{Binding MyCollection}" 
                                                                                     Style="{StaticResource EquipmentListStyle}" HorizontalAlignment="Stretch" 
                                                                                     Background="WhiteSmoke" SelectionMode="Single"
                                                                                     PreviewMouseLeftButtonDown="itemsControl_PreviewMouseLeftButtonDown" 
                                                                                     ContextMenuOpening="itemsControl_ContextMenuOpening">
                                                                                <ListBox.Resources>
                                                                                    <Style TargetType="{x:Type ListBoxItem}">
                                                                                        <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                                                                                        <Setter Property="VerticalContentAlignment" Value="Top"/>
                                                                                        <Setter Property="Focusable" Value="True"></Setter>
                                                                                        <Setter Property="ContextMenu"  Value="{StaticResource PanelContextMenu}"/>
                                                                                        <EventSetter Event="LostFocus" Handler="ListBoxItem_LostFocus"/>
                                                                                        <EventSetter Event="LostKeyboardFocus" Handler="ListBoxItem_LostKeyboardFocus"/>
                                                                                    </Style>
                                                                                </ListBox.Resources>
                                                                            </ListBox>

 code behind 代码

private void ListBoxItem_LostFocus(object sender, RoutedEventArgs e)
        {
            //((SolidColorBrush)(sender as ListBoxItem).Resources["SelectionColorKey"]).Color = Colors.Transparent;
        }

        private void ListBoxItem_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {
            //ListBoxItem lbi = sender as ListBoxItem;
            //if (lbi != null)
            //{
            //    ((SolidColorBrush)lbi.Resources["SelectionColorKey"]).Color = Colors.Transparent;
            //}
            //((SolidColorBrush)(sender as ListBoxItem).Resources["SelectionColorKey"]).Color = Colors.Transparent;
        }

 显示Menu

<ContextMenu x:Key="MyContextMenu" StaysOpen="False" Opened="MyContextMenu_Opened">
                <MenuItem x:Name="menuItemEdit" Header="Edit" 
                      Click="ContextMenu_Edit_Click">
                </MenuItem>
                <MenuItem x:Name="menuItemTest" Header="Test" 
                      Click="ContextMenu_Test_Click">
                </MenuItem>

</ContextMenu>

Menu event code behind

private void ContextMenu_Edit_Click(object sender, RoutedEventArgs e)
{
}

  

WPF - EventSetter

上一篇:WPF – pass multiple parameters to a Command


下一篇:测试驱动开发TDD(六)Start Google Test in Windows