【问题标题】:C# WPF Listbox Context Menu Command not workingC# WPF 列表框上下文菜单命令不起作用
【发布时间】:2018-04-13 00:13:28
【问题描述】:

目前我正在尝试将上下文菜单添加到使用项目模板的ListBox。我能够添加上下文菜单项,但是当我尝试绑定命令时,什么也没有发生。

Main_Window 有一个数据上下文集。这是ListBox 的 XAML。我在ListView.ItemTemplate 中使用类似的绑定样式作为按钮的一部分,所以我认为这会起作用,但遗憾的是它不是。我在这里想念什么? (这里只有重要的部分代码)

        <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>

【问题讨论】:

    标签: c# wpf contextmenu listboxitem


    【解决方案1】:

    所以,我通过不解决这个问题来解决这个问题,而是使用了一种解决方法。

    本质上是这个问题:

    System.Windows.Data 错误:4:找不到绑定源 参考

    我发现上下文菜单不是可视树的一部分(对此不满意),因此无法以相同的方式访问这些元素。

    我不喜欢使用 Reflection,因此 ElementSpy 方法对我来说并不适用。我试图直接设置一个Click="some_function",这也令人惊讶地不起作用。

    我只是将整个 ListBox 包裹在 Grid 中并使用以下内容。不是真正的 MVVM,但在这一点上,我不在乎浪费了多少时间来寻找可靠的解决方案。

    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>
    

    代码隐藏:

        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);
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多