将事件处理程序分配给自定义样式中定义的ContextMenu项

时间:2017-01-25 16:45:30

标签: wpf contextmenu datatemplate

我已创建了带有预定义MenuItems的上下文菜单样式:

    <Style TargetType="{x:Type ContextMenu}" x:Key="DataGridColumnFilterContextMenu">                
                <Setter Property="ContextMenu.Template">
                <Setter.Value>
                    <ControlTemplate>
                        <Border BorderBrush="#868686"
                                BorderThickness="1"
                                Background="#FAFAFA"> 
                                <Grid MaxHeight="500">
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto"/>
                                    <RowDefinition Height="*"/>
                                </Grid.RowDefinitions>
                                <StackPanel>
                                    <MenuItem Header="Filtrēt" StaysOpenOnClick="True" Name="DynSearch">
                                        <MenuItem.Icon>
                                            <Image   RenderOptions.BitmapScalingMode="NearestNeighbor"
                                                     RenderOptions.EdgeMode="Aliased" 
                                                     Source="/Furniture;component/Resources/search4.png" />
                                        </MenuItem.Icon>
                                    </MenuItem>
                                    <Separator  Margin="20 5 20 0"  Height="2" Width="Auto" />
                                    <MenuItem Margin="5 5 0 0" StaysOpenOnClick="True" >
                                        <MenuItem.Template>
                                            <ControlTemplate >
                                                <CheckBox Padding="15 0 0 0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" >Iezīmēt/Atzīmēt visus</CheckBox>
                                            </ControlTemplate>
                                        </MenuItem.Template>
                                        <MenuItem.Icon>
                                            <Image   RenderOptions.BitmapScalingMode="NearestNeighbor"
                                                     RenderOptions.EdgeMode="Aliased" 
                                                     Source="/Furniture;component/Resources/search4.png" />
                                        </MenuItem.Icon>
                                    </MenuItem>
                                    <Separator Margin="20 5 20 0" Height="2" Width="Auto" />
                                    <RadioButton Margin="5 5 0 0" Padding="15 0 0 0" GroupName="Sorting" Content="Augoša secībā" IsChecked="True"/>
                                    <RadioButton Margin="5 5 0 0" Padding="15 0 0 0" GroupName="Sorting" Content="Dilstoša secībā" />
                                    <Separator Margin="20 5 20 5" Height="2" Width="Auto" />
                                </StackPanel>
                                <ScrollViewer Grid.Row="1" 
                                              Margin="1,0,1,0"                                            
                                              CanContentScroll="True"
                                              VerticalScrollBarVisibility="Auto"
                                              Grid.ColumnSpan="2">
                                        <ItemsPresenter KeyboardNavigation.DirectionalNavigation="Cycle" />
                                    </ScrollViewer>
                                </Grid>                              
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

现在我将样式分配给新的ContextMenu,并希望将Click事件处理程序分配给样式中定义的MenuItem,标题为“Filtrēt”。我尝试过这样做:

ContextMenu cm = new ContextMenu();
cm.Style = Application.Current.Resources["DataGridColumnFilterContextMenu"] as Style;
var controlList = ((((cm.Template.LoadContent() as Border).Child as Grid).Children)[0] as StackPanel).Children;
MenuItem filterItem = (controlList[0] as MenuItem);
filterItem.Click += MiFiltre_Click;

当我点击调试器它没有来到MiFiltre_Click方法。我尝试了不同的事件,如MouseDown和PreviewMouseDown。此外,我试图绑定ICommand,这也没有用。

然后我搜索了这个问题并意识到cm.Template.LoadContent()可能实际上创建了我的ContextMenu模板的新实例,并且我试图将我的事件处理程序绑定到不同的控件实例。然后我尝试使用VisualTreeHelper和LogicalTreeHelper获取ContextMenu节点控件,这也不起作用。

所以这里有问题:

  1. 如何实现Click事件处理程序绑定?
  2. 如果第一个问题也是如此 很难,那么如何定义ContextMenu子节点的当前实例 在自定义风格?

1 个答案:

答案 0 :(得分:0)

我将假设此菜单样式在名为&#34; MyResources.xaml&#34;的资源字典中定义。

  1. 在同一项目文件夹中创建一个类文件,并将其命名为&#34; MyResources.xaml.cs&#34;。

  2. 将它设为像这样的部分类:

    namespace WhateverNamespace
    {
        public partial class MyResources
        {
            private void FilterMenuItem_Click(object sender, RoutedEventArgs e)
            {
                MenuItem mi = (MenuItem)sender;
    
                //  Below, PlacementTarget is the control the user right-clicked on. 
                //  Use that directly if you want that instead of its viewmodel. 
    
                //  This works with the conventional ContextMenu defined below.
                //ViewModel viewModel = ((FrameworkElement)((ContextMenu)mi.Parent).PlacementTarget).DataContext as ViewModel;
    
                //  This works with your template design. The only difference is 
                //  TemplatedParent instead of Parent. 
                ViewModel viewModel = 
                    ((FrameworkElement)((ContextMenu)mi.TemplatedParent).PlacementTarget)
                    .DataContext as ViewModel;
    
                MessageBox.Show("FilterMenuItem_Click");
            }
        }
    }
    
  3. 为资源字典提供x:Class属性:

    <ResourceDictionary 
        x:Class="WhateverNamespace.MyResources"
    
  4. 连接处理程序:

    <MenuItem 
        Click="FilterMenuItem_Click"
        Header="Filtrēt" 
        StaysOpenOnClick="True" 
        Name="DynSearch">
    
  5. 顺便说一句,您不需要使用样式和模板来创建ContextMenu资源:

    <ContextMenu x:Key="DataGridColumnFilterContextMenu">
        <MenuItem 
            Click="FilterMenuItem_Click"
            Header="Filtrēt" 
            StaysOpenOnClick="True" 
            Name="DynSearch">
            <MenuItem.Icon>
                <Image
                    RenderOptions.BitmapScalingMode="NearestNeighbor"
                    RenderOptions.EdgeMode="Aliased" 
                    Source="/Furniture;component/Resources/search4.png"
                    />
            </MenuItem.Icon>
        </MenuItem>
        <Separator />
        <MenuItem StaysOpenOnClick="True">
            <MenuItem.Header>
                <CheckBox>Iezīmēt/Atzīmēt visus</CheckBox>
            </MenuItem.Header>
            <MenuItem.Icon>
                <Image   
                    RenderOptions.BitmapScalingMode="NearestNeighbor"
                    RenderOptions.EdgeMode="Aliased" 
                    Source="/Furniture;component/Resources/search4.png"
                    />
            </MenuItem.Icon>
        </MenuItem>
        <Separator />
        <MenuItem>
            <MenuItem.Header>
                <RadioButton GroupName="Sorting" Content="Augoša secībā" IsChecked="True"/>
            </MenuItem.Header>
        </MenuItem>
        <MenuItem>
            <MenuItem.Header>
                <RadioButton GroupName="Sorting" Content="Dilstoša secībā" IsChecked="True"/>
            </MenuItem.Header>
        </MenuItem>
    </ContextMenu>