两个上下文菜单共享图标,但只显示一个上下文菜单图标

时间:2015-11-19 17:17:56

标签: c# wpf mvvm

我有两个Context Menus如下,两个都有相同的syntax,并且是不同控件的资源。

   <ContextMenu ItemsSource="{Binding Actions}">
       <ContextMenu.ItemContainerStyle>
           <Style TargetType="{x:Type MenuItem}" BasedOn="{StaticResource MetroMenuItem}">
               <Setter Property="Header" Value="{Binding Title}"/>
               <Setter Property="ToolTip" Value="{Binding ToolTips}"/>
               <Setter Property="Command" Value="{Binding Command}"/>
               <Setter Property="Icon" Value="{Binding Icon}"/>
               <Setter Property="CommandParameter" Value="{Binding CommandParameter}"/>
           </Style>
       </ContextMenu.ItemContainerStyle>
   </ContextMenu>

一切正常,但只有Icon问题。

视图模型

我有以下属性,

 public Image Icon
    {
        get { return _Icon; }
        set{ _Icon = value; NotifyPropertyChanged(); }
    }

我正在初始化它,如下所示,

   Icon = new Image
        {
            Source = new BitmapImage(new Uri(@"../images/ReIndex.png", UriKind.Relative)),
            Height = 20,
            Width = 20,
            Margin = new Thickness(5)
        };

问题是,如果一个上下文菜单显示另一个上下的图标。

我知道MenuItem.IconObject。所以我尝试直接使用BitmapImage而不是Image,但我仍有问题。

修改/解决

添加了如下资源

 <Control.Resources>        
     <Image x:Shared="False" x:Key="Icon" Source="{Binding Icon}" Height="20" Width="20"/>
 </Control.Resources>

然后我的上下文菜单如下所示

<ContextMenu ItemsSource="{Binding Actions}">
    <ContextMenu.ItemContainerStyle>
        <Style TargetType="{x:Type MenuItem}" BasedOn="{StaticResource MetroMenuItem}">
            <Setter Property="Header" Value="{Binding Title}"/>
            <Setter Property="ToolTip" Value="{Binding ToolTips}"/>
            <Setter Property="Command" Value="{Binding Command}"/>
            <Setter Property="Icon" Value="{StaticResource Icon}"/>
            <Setter Property="CommandParameter" Value="{Binding CommandParameter}"/>
        </Style>
    </ContextMenu.ItemContainerStyle>
</ContextMenu>

ViewModel 现在

public BitmapImage Icon
{
    get { return _Icon; }
    set{ _Icon = value; NotifyPropertyChanged(); }
}

Icon = new BitmapImage(new Uri(@"../images/Pencil-01.png", UriKind.Relative));

x:Shared="False"控件中的要点为Image

如果有任何其他好的解决方案表示赞赏。

1 个答案:

答案 0 :(得分:0)

添加了如下资源

// Objective-C
- (void)didTapButton {
    Digits *digits = [Digits sharedInstance];
    DGTAuthenticationConfiguration *configuration = [[DGTAuthenticationConfiguration alloc] initWithAccountFields:DGTAccountFieldsDefaultOptionMask];
    configuration.phoneNumber = @"+345555555555";
    [digits authenticateWithViewController:nil configuration:configuration completion:^(DGTSession *newSession, NSError *error){
    // Country selector will be set to Spain and phone number field will be set to 5555555555
}];

然后我的上下文菜单如下所示

- (void)authenticateWithPhoneNumber:(twtr_nullable NSString *)phoneNumber digitsAppearance:(twtr_nullable DGTAppearance *)appearance 
viewController:(twtr_nullable UIViewController *)viewController
title:(twtr_nullable NSString *)title completion:(DGTAuthenticationCompletion)completion 
__attribute__((deprecated("Use authenticateWithViewController:configuration:completion: instead.")));

<强>视图模型

<Control.Resources>        
    <Image x:Shared="False" x:Key="Icon" Source="{Binding Icon}" Height="20" Width="20"/>
</Control.Resources>

<ContextMenu ItemsSource="{Binding Actions}"> <ContextMenu.ItemContainerStyle> <Style TargetType="{x:Type MenuItem}" BasedOn="{StaticResource MetroMenuItem}"> <Setter Property="Header" Value="{Binding Title}"/> <Setter Property="ToolTip" Value="{Binding ToolTips}"/> <Setter Property="Command" Value="{Binding Command}"/> <Setter Property="Icon" Value="{StaticResource Icon}"/> <Setter Property="CommandParameter" Value="{Binding CommandParameter}"/> </Style> </ContextMenu.ItemContainerStyle> </ContextMenu> 控制中的主要观点为public BitmapImage Icon { get { return _Icon; } set{ _Icon = value; NotifyPropertyChanged(); } } Icon = new BitmapImage(new Uri(@"../images/Pencil-01.png", UriKind.Relative));