简体   繁体   English

C#WPF列表框上下文菜单命令不起作用

[英]C# WPF Listbox Context Menu Command not working

Currently I am trying to add a context menu to a ListBox that uses an item template. 目前,我正在尝试将上下文菜单添加到使用项目模板的ListBox中。 I am able to get the context menu items added, but when I try to bind the commands, nothing happens. 我可以添加上下文菜单项,但是当我尝试绑定命令时,什么也没有发生。

The Main_Window has a data context set. Main_Window具有数据上下文集。 Here is the XAML for the ListBox . 这是ListBox的XAML。 I use a similar Binding style as part of a button in the ListView.ItemTemplate so I would assume this would work, but sadly it is not. 我在ListView.ItemTemplate使用类似的Binding样式作为按钮的一部分,因此我认为这是可行的,但不幸的是,它不是。 What am I missing here? 我在这里想念什么? (Only important part of the code is here) (这里只有代码的重要部分)

        <ListBox x:Name="company_buttons_listbox"
                 ItemsSource="{Binding Buttons_Binding}"
                 SelectedIndex="{Binding Selected_Index, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}">

            <ListBox.Resources>
                <Style TargetType="{x:Type ListBoxItem}">
                    <Setter Property="ContextMenu">
                        <Setter.Value>
                            <ContextMenu>
                                <MenuItem Header="Update Frazer Server Link" Foreground="Black" FontFamily="Segoe UI" FontSize="14" FontWeight="Bold"
                                          CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=Window}}"
                                          Command="{Binding ElementName=Main_Window, Path=DataContext.Testing}"/>
                            </ContextMenu>
                        </Setter.Value>
                    </Setter>
                    <Style.Resources>
                        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="LightSteelBlue" Opacity="0.5"/>
                        <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="LightSteelBlue" Opacity="0.5"/>
                    </Style.Resources>
                </Style>
                <Style TargetType="{x:Type ListBox}">
                    <Setter Property="KeyboardNavigation.TabNavigation" Value="Continue"/>
                </Style>
            </ListBox.Resources>

            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Margin="-2,0,-2,0">

                        <Button CommandParameter="{Binding}"
                                Command="{Binding ElementName=Main_Window, Path=DataContext.Open_Link}">
                        </Button>

                        <Label VerticalContentAlignment="Top"
                               Margin="5,0,5,0" Height="19" Padding="0"
                               Foreground="White" FontFamily="Segoe UI" FontSize="12" FontWeight="Bold"
                               Content="{Binding ItemText}"/>

                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

So, I solved this by not solving this and instead used a work around. 因此,我通过不解决此问题来解决此问题,而是使用了替代方法。

Essentially the issue comes from this: 本质上,问题是由以下原因引起的:

System.Windows.Data Error: 4 : Cannot find source for binding with reference System.Windows.Data错误:4:找不到参考源进行绑定

I found that Context Menus are not part of the Visual Tree (not happy about that) and hence cannot access those elements in the same fashion. 我发现上下文菜单不是可视树的一部分(对此不满意),因此无法以相同的方式访问这些元素。

I am not a fan of using Reflection so the ElementSpy method is off the table for me along with. 我不喜欢使用Reflection,因此ElementSpy方法对我来说并不适用。 I tried to directly set a Click="some_function" and that also surprisingly DID not work. 我试图直接设置一个Click="some_function" ,而且令人惊讶的是DID无法正常工作。

I instead just wrapped my entire ListBox in a Grid and used the following. 相反,我只是将我的整个ListBox包装在一个Grid并使用了以下内容。 Not really MVVM, but I could care less at this point with how much wasted time I put into finding a solid and reliable solution. 并不是真正的MVVM,但是在这一点上,我可以花点时间在寻找可靠而可靠的解决方案上花费多少时间。

XAML: XAML:

            <Grid.ContextMenu>
                <ContextMenu>
                    <MenuItem Header="Menu Item Text" Foreground="Black" FontFamily="Segoe UI" FontSize="14" FontWeight="Bold"
                              Click="menu_item_function"/>
                    <Separator/>
            </Grid.ContextMenu>

Code Behind: 背后的代码:

    private void menu_item_function(object sender, RoutedEventArgs e)
    {
        // Get the viewmodel from the DataContext
        MainWindowViewModel viewmodel = DataContext as MainWindowViewModel;

        // Call command from viewmodel     
        if ((viewmodel != null) && (viewmodel.View_Model_Function.CanExecute(this)))
        {
            viewmodel.View_Model_Function.Execute(this);
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM