开发者

Databound ContextMenu Separator not honoring global Separator Style

开发者 https://www.devze.com 2023-01-08 12:52 出处:网络
I have a context menu bound to a data source. For this context menu I have a DataTrigger to display a separator if the databound object has a value of \"True\" for the Separator property. This works w

I have a context menu bound to a data source. For this context menu I have a DataTrigger to display a separator if the databound object has a value of "True" for the Separator property. This works well however it doesn't seem to pick up my global style for separators that I have in my application. The new separator appearance is different than the rest of my menu's. Is there a way to have it use the global style?

Below is the ContextMenu definition:

<ContextMenu  
  x:Key="ActionMenu" 
  ItemsSource="{Binding Source={StaticResource ActionMenuSource}}">
      <ContextMenu.ItemContainerStyle> 
          <Style TargetType="{x:Type MenuItem}" BasedOn="{StaticResource {x:Type MenuItem}}"> 
          <Style.Triggers> 
              <DataTrigger Binding="{Binding Path=Separator}" Value="true"> 
                  <Setter Property="MenuItem.Template"> 
                      <Setter.Value> 
                          <ControlTemplate TargetType="{x:Type MenuItem}"> 
                              <Separator Style="{DynamicResource {x:Static             
                                         MenuItem.SeparatorStyleKey}}"/> 
                          </ControlTemplate> 
                      </Setter.Value> 
                  </Setter> 
              </DataTrigger> 
          </Style.Triggers> 
      </Style> 
   </ContextMenu.ItemContainerStyle> 
</ContextMenu>

Here are my global values for defining the Separator.

<Style x:Key="{x:Static MenuItem.SeparatorStyleKey}" TargetType="{x:Type Separator}">
  <Setter Property="Template" Value="{DynamicResource tmp_ManhMenuItemSeparator}"/>
</Style>

<ControlTemplate x:Key="tmp_ManhMenuItemSeparator" TargetType="{x:Type Separator}">
  <Rectangle N开发者_如何学编程ame="SepRect" StrokeThickness="1" Stroke="White"/>
</ControlTemplate>


To make a style global the key needs to be the same as the TargetType which is clearly not the case in your sample. Change your key to "{x:Type Separator}" and see if that works for you. You can also merge your two snippets together unless you have a particular need to split them out, e.g.

<Style x:Key="{x:Type Separator}" TargetType="{x:Type Separator}">
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="{x:Type Separator}">
        <Rectangle Name="SepRect" StrokeThickness="1" Stroke="White"/>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

Once you have done that you can probably strip down the first block of code. It looks like you are trying to make the menu manually use the style. If you make it global you don't need to do that, e.g.

    <ContextMenu x:Key="ActionMenu" ItemsSource="{Binding Source={StaticResource ActionMenuSource}}"/>
0

精彩评论

暂无评论...
验证码 换一张
取 消