上下文菜单的名称标题

时间:2015-10-19 13:31:41

标签: c# wpf image contextmenu

标题似乎比最初想象的要容易,但我有一个绑定到项目列表的上下文菜单。我希望能够显示上下文菜单的标题以访问列表。目前它只显示为空白然后允许我查看列表,但是我需要它来显示标题“添加现有属性”。此外,如果有人可以帮助我解决我的图像问题,将非常感激:)。而不是第一个具有图像名称的我希望它显示名称“添加现有属性”

<DataTemplate x:Key="AddNodeTemplate">
        <Border BorderThickness="1" Background="#F7F7F7">
            <Border.BorderBrush>
                <DrawingBrush Viewport="8,8,8,8" ViewportUnits="Absolute" TileMode="Tile">
                    <DrawingBrush.Drawing>
                        <DrawingGroup>
                            <GeometryDrawing Brush="#F7F7F7">
                                <GeometryDrawing.Geometry>
                                    <GeometryGroup>
                                        <RectangleGeometry Rect="0,0,50,50"/>
                                        <RectangleGeometry Rect="50,50,50,50"/>
                                    </GeometryGroup>
                                </GeometryDrawing.Geometry>
                            </GeometryDrawing>
                        </DrawingGroup>
                    </DrawingBrush.Drawing>
                </DrawingBrush>
            </Border.BorderBrush>
            <StackPanel>
                <Button x:Name="ButtonAdd" Click="ButtonAdd_Click" Height="30" Width="130">
                    <Button.Style>
                        <Style TargetType="Button">
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate TargetType="Button">
                                        <Grid Background="#F7F7F7">
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="25"/>
                                                <ColumnDefinition/>
                                            </Grid.ColumnDefinitions>
                                            <Image Source="Images/icon_plus.bmp" HorizontalAlignment="Left" Margin="5,0,0,0"/>
                                            <TextBlock Text="Add Property" HorizontalAlignment="Center" Grid.Column="1" VerticalAlignment="Center" Foreground="LightGray" FontStyle="Italic" FontSize="12"/>
                                        </Grid>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                            <Setter Property="Background" Value="#F7F7F7"/>
                        </Style>
                    </Button.Style>
                    <Button.ContextMenu>
                        <ContextMenu>
                            <MenuItem Header="Add Exisiting Properties" ItemsSource="{Binding Path=AvailableProperties}">
                                    <MenuItem.Resources>
                                        <Style TargetType="MenuItem">
                                            <Setter Property="HeaderTemplate">
                                                <Setter.Value>
                                                    <DataTemplate>
                                                        <StackPanel Orientation="Horizontal">                                                                
                                                            <ContentPresenter Content="{Binding Name}" />
                                                        </StackPanel>
                                                    </DataTemplate>
                                                </Setter.Value>
                                            </Setter>
                                        <Setter Property="Icon">
                                            <Setter.Value>
                                                <DataTemplate>
                                                    <StackPanel>
                                                        <Image Width="12" Height="12" Source="{Binding Icon, Converter={StaticResource ImageToSourceConverter}}" Margin="3" VerticalAlignment="Center"/>
                                                    </StackPanel>
                                                </DataTemplate>
                                            </Setter.Value>
                                        </Setter>
                                    </Style>
                                    </MenuItem.Resources>
                            </MenuItem>
                            <MenuItem Header="Upscale well logs"/>
                            <MenuItem Header="Upscale well_top attributes"/>
                            <MenuItem Header="Upscale point attributes"/>
                            <MenuItem Header="Calculate"/>
                        </ContextMenu>
                    </Button.ContextMenu>
                </Button>
        </StackPanel>
        </Border>
    </DataTemplate>

显示的内容

enter image description here

2 个答案:

答案 0 :(得分:2)

此样式<Style TargetType="MenuItem">实际上 适用于顶级MenuItem(您将其Header硬编码为Add Exisiting Properties的那个})。您需要使用Trigger根据Role属性对其进行过滤。所有子菜单项都Role SubMenuItem,所以它应该是这样的:

 <Style TargetType="MenuItem">
     <Style.Triggers>
        <Trigger Property="Role" Value="SubMenuItem">
             <Setter Property="HeaderTemplate">
                    <Setter.Value>
                           <DataTemplate>
                             <StackPanel Orientation="Horizontal">
                               <ContentPresenter Content="{Binding Name}"/>
                             </StackPanel>
                           </DataTemplate>
                    </Setter.Value>
             </Setter>
        </Trigger>
     </Style.Triggers>
     <!-- ... -->
 </Style>        

编辑 :关于图标,您明确将其设置为未呈现的DataTemplate,而是使用{{1}返回的字符串(因此文本ToString()代替图标)。你可以像这样删除System.Windows.DataTemplate

DataTemplate

最终解决方案:

<!-- define a non-shared resource of Image -->
<Style TargetType="MenuItem">
   <Style.Resources>
      <Image x:Key="img" x:Shared="False" Width="12" Height="12" Source="{Binding Icon, Converter={StaticResource ImageToSourceConverter}}" 
             Margin="3" VerticalAlignment="Center"/>
   </Style.Resources>
   <!-- ... -->
</Style>

<Setter Property="Icon" Value="{StaticResource img}"/>

答案 1 :(得分:0)

你可以使用 `

<MenuItem Header="Add Exisiting Properties" ItemsSource="{Binding Path=AvailableProperties}">
    <MenuItem.Resources>
      <Style TargetType="MenuItem">
         <Setter Property="HeaderTemplate">
              <Setter.Value>
                 <DataTemplate>
                     <StackPanel Orientation="Horizontal">                                                                  
<ContentPresenter Content="{TemplateBinding Header}" />
                     </StackPanel>
                 </DataTemplate>
              </Setter.Value>
         </Setter>

or

<MenuItem Header="Add Exisiting Properties" ItemsSource="{Binding Path=AvailableProperties}">
            <MenuItem.Resources>
              <Style TargetType="MenuItem">
                 <Setter Property="HeaderTemplate">
                   <Setter.Value>
                       <DataTemplate>
                         <StackPanel Orientation="Horizontal">                                                                     
    <ContentPresenter Content="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Header}" />
                         </StackPanel>
                       </DataTemplate>
                    </Setter.Value>
                 </Setter>

`