2013-09-22 2 views
2

ContextMenu을 사용하는 WPF Window 응용 프로그램에서 작업하고 있습니다. 'MenuItem'유형의 스타일을 '구분 기호'유형에 적용 할 수 없습니다.

ContextMenu (Window.Resources에서) XAML에서 : 나는 ContextMenu에 구분을 추가하려고하면

<ContextMenu x:Key="menuList" Placement="Bottom" > 
    <ContextMenu.ItemContainerStyle> 
     <Style TargetType="{x:Type MenuItem}"> 
      <Setter Property="Header" Value="{Binding Name}"/> 
      <EventSetter Event="Click" Handler="cm_RefreshChannelNotification"/> 
      <Setter Property="IsChecked" Value="{Binding CFiltered}" /> 
      <Setter Property="IsCheckable" Value="True"/> 
      <Setter Property="StaysOpenOnClick" Value="True"/>   
     </Style> 
    </ContextMenu.ItemContainerStyle> 
</ContextMenu> 

내가 오류가 나타납니다 이러한 방법으로

System.InvalidOperationException was unhandled Message="A style intended for type 'MenuItem' cannot be applied to type 'Separator'.

을 나는 새를 추가해야합니다 구분 :

ContextMenu cm = this.FindResource("menuList") as ContextMenu; 
Separator separator = new Separator(); 
separator.SnapsToDevicePixels = true; 
cm.Items.Add(separator); 

나는 ContextMenu에 추가/무엇을 변경해야 정의가 작동하도록하려면?

답변

5

스타일을 ContextMenu.Resources으로 옮길 수 있습니다. Separators과 충돌하지 않는 메뉴 항목에 암시 적으로 적용됩니다.


대안이 TargetType을 삭제하고 속성을 한정 할 수있다, 비 적용 특성이 무시 될 수 있습니다. 그래도이 방법을 사용하지 마십시오.

<Style> 
    <Setter Property="MenuItem.Header" Value="{Binding Name}"/> 
+0

다른 대안이 있습니다. – U62

1

하나는 H.B.의 대답에 대한 답변입니다. 두 가지 방법 모두 나를 위해 일하고 있습니다.

그러나 MenuItem.Header 속성을 설정하면 출력 창에 바인딩 오류가 발생하므로 무시할 수 있습니다.

내 생각에 스타일을 ContextMenu.Resources로 옮기는 것이 더 좋습니다.

죄송합니다. 아직이 의견을 쓸 수 없습니다.

관련 문제