【问题标题】:ContextMenu too-wide WPFContextMenu 太宽的 WPF
【发布时间】:2015-06-04 06:28:59
【问题描述】:

我已经使用 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>

看起来像:

我希望 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>   

对于错误的菜单项标题,我必须重写 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;
    }
   ....
  }

【问题讨论】:

  • 第二个:请插入您的MenuItemsList 和MenuItemViewModel 代码(看起来标题设置器无法正常工作)输出窗口中的任何错误?
  • 您的样式被覆盖或应用不正确。缺少标头的二传手。 MenuItemViewModel 中缺少名称属性。可能的解决方法是覆盖 MenuItemViewModel 的 ToString,但我认为您的命令保持“模糊”...
  • 我遇到了这个人的问题,我认为这就是原因:stackoverflow.com/questions/6177550/…
  • 那么你的问题是什么,图标还是文字?
  • 这意味着我只有最后一个 MenuItem 的图标。我想如果我解决了这个问题,我不会在 ContextMenu 中看到 TreeView.....

标签: wpf mvvm contextmenu


【解决方案1】:

MenuItem 中的DataTemplate 未按预期工作。 我使用了 DataTemplate 的 Style:

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      不要在 DataTemplate 中使用 MenuItem,而是使用带有 BorderThickness="0" Background="Transparent" 的按钮

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-09
        • 2010-10-05
        • 2012-06-09
        相关资源
        最近更新 更多