上下文菜单 too-wide WPF

ContextMenu too-wide WPF

我已经使用 MVVM 动态构建了一个 ContextMenu。问题是:MenuItems 的内容都在右侧 ==> ContextMenu 太宽了。你有什么想法吗?谢谢

这是XAML中的代码:

<TreeView.ContextMenu>
            <ContextMenu Name="RightClickMenu" ItemsSource="{Binding Path=SelectedItem.MenuItemsList}">
                <ContextMenu.ItemTemplate >
                    <DataTemplate>
                    <!-- <MenuItem HorizontalAlignment="Left" Header="{Binding Name}" Command="{Binding Command}" -->
                        <StackPanel Orientation="Horizontal">
                            <Image Source="{Binding MyIcon}"  Width="18" Height="18" SnapsToDevicePixels="True" />
                        <MenuItem  Header="{Binding Name}" Command="{Binding Command}" 
                              CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, 
            AncestorType={x:Type TreeView}}, Path=DataContext.SelectedItem}"
                                  />
                            </StackPanel>
                    </DataTemplate>
                </ContextMenu.ItemTemplate>
            </ContextMenu>
        </TreeView.ContextMenu>

看起来像:

我希望上下文菜单是这样的:

第二个问题:

有时效果很好,但有时我会遇到这些奇怪的事情。

----------------------------解决了---------------- ------------------------------

对于 Sac1。我通过添加 x:Shared="False" 修改了您的解决方案。检查 MSDN 以获取 x:Shared.

     <Style TargetType="MenuItem" x:Shared="False"> 
                <Setter Property="Icon">
                    <Setter.Value>
                        <Image Source="{Binding Path=MyIcon}" Height="20" Width="20"  >
                        </Image>
                    </Setter.Value>
                </Setter>
                <Setter Property="Header" Value="{Binding Path=Name}"  />
                <Setter Property="Command" Value="{Binding Command}" />
                <Setter Property="CommandParameter" Value="{Binding RelativeSource={RelativeSource FindAncestor, 
                AncestorType={x:Type TreeView}}, Path=DataContext.SelectedItem}" />
            </Style>   

对于错误的 headers 菜单项,我不得不重写 MenuItemViewModel 中的 ToString() 方法。我不明白为什么我必须覆盖 ToString() 但它现在运行良好。

public class MenuItemViewModel : BindableBase
{
......
 public string Name
    {
        get
        {
            return model.Name;
        }
        set
        {
            this.model.Name = value;
            OnPropertyChanged("Name");
        }
    }

 public override string ToString()
    {
        return Name;
    }
   ....
  }

您不应将 DataTemplate 用于 ContextMenu。只需按照提供的方式使用它:

<ContextMenu>
    <MenuItem Command="{Binding Path=Command}"
              Icon="{Binding Path=MyIcon}"
              Header="{Binding Path=Name}"
              InputGetstureText="CTRL+O" />
</ContextMenu>
MenuItem

DataTemplate 未按预期工作。 我使用了 DataTemplate 的样式:

<TreeView.Resources>
    <Style TargetType="MenuItem">
        <Setter Property="Icon" Value="{Binding MyIcon}" />
        <Setter Property="Header" Value="{Binding Name}" />
        <Setter Property="Command" Value="{Binding Command}" />
        <Setter Property="CommandParameter" Value="{Binding RelativeSource={RelativeSource FindAncestor, 
        AncestorType={x:Type TreeView}}, Path=DataContext.SelectedItem}" />
    </Style>
</TreeView.Resources>
<TreeView.ContextMenu>
    <ContextMenu ItemsSource="{Binding Path=SelectedItem.MenuItemsList}" />
</TreeView.ContextMenu>

不要在 DataTemplate 中使用 MenuItem,而是使用 BorderThickness="0" Background="Transparent"

的按钮