在XAML中动态绑定MenuItem图标。可能吗? [英] Dynamic binding of MenuItem icon in XAML. Is it possible?

查看:100
本文介绍了在XAML中动态绑定MenuItem图标。可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试设置菜单项图标的绑定,以便根据所选项目的数量(和状态)改变它。



我'我尝试了一些图像源绑定的变体,包括下面显示的三个以及其他一些我不能在这里重复的麻烦。



没什么似乎工作。绑定没有建立,我得到的是主题的变体,无法找到与引用'RelativeSource FindAncestor绑定的源,AncestorType ='System.Windows.Controls.ListView',AncestorLevel ='1' '.BindingExpression:Path = SelectedItems; DataItem = null;目标元素是'Image'(Name ='startIcon');目标属性是'Source'(类型'ImageSource'),带有不同的错误代码(主要是4)。



我似乎错过了一些明显的东西,但我看不出它是什么。



Trying to set up the binding for a menu item's icon so that I can vary it according to the number (and state) of items selected.

I've tried a number of variations for the Image Source binding including the three shown below as well as a number of others I can't be bothered to repeat here.

Nothing seems to work. Binding is not established, all I get are variations on a theme of, "Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.ListView', AncestorLevel='1''. BindingExpression:Path=SelectedItems; DataItem=null; target element is 'Image' (Name='startIcon'); target property is 'Source' (type 'ImageSource')" with varying error codes (mainly 4).

It would seem I'm missing something obvious, but I can't see what it is.

<ListView  x:Name="grdTasks"
           ItemContainerStyle="{StaticResource SimpleListBoxItem}"
           DockPanel.Dock="Top"
           HorizontalAlignment="Stretch"
           ScrollViewer.HorizontalScrollBarVisibility="Disabled"
           ScrollViewer.VerticalScrollBarVisibility="Auto"
           HorizontalContentAlignment="Stretch"
           VerticalContentAlignment="Stretch" Margin="2">

  <ListView.ContextMenu>
    <ContextMenu  x:Name="ItemsMenu"
      DataContext="{Binding Path=PlacementTarget.DataContext,
                    RelativeSource={RelativeSource Self}}" >
      <MenuItem x:Name="start"
                Header="Start"
                Command="{Binding Path=StartCommand}"
                CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=ContextMenu},
                                           Path=PlacementTarget.SelectedItems}">

        <MenuItem.Icon>
          <Image x:Name="startIcon" Height="16" Width="16"
                 Source="{Binding RelativeSource={RelativeSource AncestorType=ContextMenu, AncestorLevel=2},
                                                  Path=PlacementTarget.SelectedItems,
                                                  Converter={StaticResource GroupImageConverter}}">
          </Image>
        </MenuItem.Icon>


      </MenuItem>
    </ContextMenu>
  </ListView.ContextMenu>

</ListView>





...以及定位命名元素。



...as well as targetting a named element.

<MenuItem.Icon>
  <Image x:Name="startIcon" Height="16" Width="16"
         Source="{Binding ElementName=grdTasks,
                          Path=SelectedItems,
                          Converter={StaticResource GroupImageConverter}}">
  </Image>
</MenuItem.Icon>



。 ..还有这个。虽然我有一半期望这不起作用,因为(根据我们最喜欢的搜索工具),上下文菜单不是视觉树的一部分。




...and this. Although I'd half expect this not to work because (according to our favourite search tool), "context menu is not part of the visual tree".

<MenuItem.Icon>
  <Image x:Name="startIcon" Height="16" Width="16"
         Source="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListView}},
                                          Path=SelectedItems,
                                          Converter={StaticResource GroupImageConverter}}">
  </Image>
</MenuItem.Icon>

推荐答案

这篇关于在XAML中动态绑定MenuItem图标。可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆