与itemsource绑定的上下文菜单不起作用

时间:2014-02-28 09:01:00

标签: wpf xaml wpf-controls

在一个按钮中,我试图将上下文菜单放在我需要绑定标题和来自itemsource的图标的位置。 但这并没有成功,并在运行时给出一些错误。无法弄清楚问题。

<ContextMenu Width="Auto"
             ItemsSource="{Binding Path=ListofTransmitModes}"
             Placement="Top">
    <ContextMenu>
        <MenuItem>
            <MenuItem.Header>
               <TextBlock Text="{Binding ModeName}"/>
            </MenuItem.Header>
        <MenuItem.Icon>
        <DataTemplate>
            <Image>
               <Image.Style>
                   <Style TargetType="{x:Type Image}">
                       <Setter Property="Stretch" Value="Uniform" />
                       <Setter Property="Width" Value="17" />
                       <Setter Property="Margin" Value="3,3,6,3"/>
                       <Setter Property="RenderOptions.BitmapScalingMode" Value="NearestNeighbor" />
                   <Style.Triggers>
                      <Trigger Property="IsEnabled" Value="False">
                          <Setter Property="Opacity" Value="0.24" />
                      </Trigger>
                      <DataTrigger Binding="{Binding ModeName}" Value="{x:Static p:Resource.ANALOG_CLEAR}">
                         <Setter Property="Source" Value="/Motorola.APS.MMC.Common.Themes;component/Images/LMR/analog_clear.png" />
                      </DataTrigger>
                      <DataTrigger Binding="{Binding ModeName}" Value="{x:Static p:Resource.ANALOG_CODED}">
                          <Setter Property="Source" Value="/Motorola.APS.MMC.Common.Themes;component/Images/LMR/analog_coded.png" />
                      </DataTrigger>
                      <DataTrigger Binding="{Binding ModeName}" Value="{x:Static p:Resource.DIGITAL_CLEAR}">
                          <Setter Property="Source" Value="/Motorola.APS.MMC.Common.Themes;component/Images/LMR/digital_clear.png" />
                      </DataTrigger>
                      <DataTrigger Binding="{Binding ModeName}" Value="{x:Static p:Resource.DIGITAL_CODED}">
                          <Setter Property="Source" Value="/Motorola.APS.MMC.Common.Themes;component/Images/LMR/digital_coded.png" />
                      </DataTrigger>
                  </Style.Triggers>
              </Style>
           </Image.Style>
        </Image>
     </DataTemplate>
     </MenuItem.Icon>
     </MenuItem>
    </ContextMenu>
   <ContextMenu.ItemContainerStyle>

   <Style TargetType="MenuItem">
        <Setter Property="MenuItem.Command" Value="{Binding Path=DataContext.TransmitModeSelected, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ContextMenu}}}" />
        <Setter Property="MenuItem.CommandParameter" Value="{Binding}" />
   </Style>
   </ContextMenu.ItemContainerStyle>
   </ContextMenu>

2 个答案:

答案 0 :(得分:0)

用此替换你的,

       <Button.ContextMenu>
            <ContextMenu Width="Auto"
                         ItemsSource="{Binding Path=ListofTransmitModes}"
                         Placement="Top">
                <ContextMenu.ItemTemplate>
                    <DataTemplate>
                        <MenuItem Header="{Binding ModeName}">
                            <MenuItem.Icon>
                                <Image>
                                    <Image.Style>
                                        <Style TargetType="{x:Type Image}">
                                            <Setter Property="Stretch" Value="Uniform" />
                                            <Setter Property="Width" Value="17" />
                                            <Setter Property="Margin" Value="3,3,6,3" />
                                            <Setter Property="RenderOptions.BitmapScalingMode" Value="NearestNeighbor" />
                                            <Style.Triggers>
                                                <Trigger Property="IsEnabled" Value="False">
                                                    <Setter Property="Opacity" Value="0.24" />
                                                </Trigger>
                                                <DataTrigger Binding="{Binding ModeName}" Value="{x:Static p:Resource.ANALOG_CLEAR}">
                                                    <Setter Property="Source" Value="/Motorola.APS.MMC.Common.Themes;component/Images/LMR/analog_clear.png" />
                                                </DataTrigger>
                                                <DataTrigger Binding="{Binding ModeName}" Value="{x:Static p:Resource.ANALOG_CODED}">
                                                    <Setter Property="Source" Value="/Motorola.APS.MMC.Common.Themes;component/Images/LMR/analog_coded.png" />
                                                </DataTrigger>
                                                <DataTrigger Binding="{Binding ModeName}" Value="{x:Static p:Resource.DIGITAL_CLEAR}">
                                                    <Setter Property="Source" Value="/Motorola.APS.MMC.Common.Themes;component/Images/LMR/digital_clear.png" />
                                                </DataTrigger>
                                                <DataTrigger Binding="{Binding ModeName}" Value="{x:Static p:Resource.DIGITAL_CODED}">
                                                    <Setter Property="Source" Value="/Motorola.APS.MMC.Common.Themes;component/Images/LMR/digital_coded.png" />
                                                </DataTrigger>
                                            </Style.Triggers>
                                        </Style>
                                    </Image.Style>
                                </Image>
                            </MenuItem.Icon>
                        </MenuItem>
                    </DataTemplate>
                </ContextMenu.ItemTemplate>
                <ContextMenu.ItemContainerStyle>
                    <Style TargetType="MenuItem">
                        <Setter Property="MenuItem.Command" Value="{Binding Path=DataContext.TransmitModeSelected, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ContextMenu}}}" />
                        <Setter Property="MenuItem.CommandParameter" Value="{Binding}" />
                    </Style>
                </ContextMenu.ItemContainerStyle>
            </ContextMenu>
        </Button.ContextMenu>

答案 1 :(得分:0)

您的问题是ContextMenu显示在与UI其余部分不同的可视树中。因此,它无法从主UI可视树中访问DataContext。出于这个原因,我们必须使用FrameworkElement.Tag属性和ContextMenu.PlacementTarget property将其传递给另一个可视化树。首先,将视图的DataContext或您想要的任何内容设置到您要设置Tag的控件的ContextMenu属性中:

<Button Tag="{Binding DataContext, RelativeSource={RelativeSource AncestorType={
    x:Type YourViewsXmlNamespace:YourView}}}" ContextMenu="{StaticResource Menu}" ... >
    ...
</Button>

现在您可以通过ContextMenu.PlacementTarget属性

访问此内容
<ContextMenu ItemsSource="{Binding Path=ListofTransmitModes}" Placement="Top"
    DataContext="{Binding PlacementTarget.Tag, RelativeSource={RelativeSource Self}}">
    ...
</ContextMenu>