码迷,mamicode.com
首页 > Windows程序 > 详细

WPF - EventSetter

时间:2014-12-03 16:57:56      阅读:274      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   ar   color   os   sp   on   div   

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

标签:style   blog   io   ar   color   os   sp   on   div   

原文地址:http://www.cnblogs.com/mantian/p/4140377.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!