2012-03-07 2 views
1

ContexMenu의 기본 스타일을 설정하려고하는데 스타일의 기본 GroupStyle을 ContexMenu로 설정하고 싶습니다. 다음과 같은 것 :xaml에서 스타일 안에 GroupStyle을 설정하십시오.

<Setter Property="ItemsControl.GroupStyle"> 
    <Setter.Value> 
     <GroupStyle> 
      <...> 
     </GroupStyle> 
    </Setter.Value> 
</Setter> 

컴파일러는 오류 : ItemsControl에서 GroupStyle을 찾을 수 없다고 말합니다.

그러나 코드에서 나는 간단하게 수행 할 수 있습니다

ContextMenu contextMenu; 
contextMenu.GroupStyle.Add(someSavedStyle); 

어떻게이 XAML에서 얻을 수 있습니까?

미리 감사드립니다.

답변

0

사실, 몇 가지 추가 작업으로 수행 할 수 있습니다 :
대신 ItemsPresenterContexMenu의 템플릿을 설정, 당신은 당신의 데이터를 맞는 제어를 설정할 수 있습니다. 이 경우 Menu으로 설정할 수 있습니다. 그냥 같이 : 이제

<Style TargetType="ContextMenu"> 
    <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="ContextMenu"> 
         <Border> 
          <Menu ItemsSource="{TemplateBinding ItemsSource}"> 
           <Menu.GroupStyle> 
            <GroupStyle> 
             <GroupStyle.ContainerStyle> 
              <Style TargetType="{x:Type GroupItem}"> 
               <Setter Property="Template"> 
                <Setter.Value> 
               <ControlTemplate TargetType="{x:Type GroupItem}"> 
                  <StackPanel> 
                   <Border Background="Black"> 
                    <ItemsPresenter/> 
                   </Border> 
                  </StackPanel> 
                 </ControlTemplate> 
                </Setter.Value> 
               </Setter> 
              </Style> 
             </GroupStyle.ContainerStyle> 
            </GroupStyle> 
           </Menu.GroupStyle> 
           <Menu.ItemsPanel> 
            <ItemsPanelTemplate> 
             <StackPanel></StackPanel> 
            </ItemsPanelTemplate> 
           </Menu.ItemsPanel> 
          </Menu> 
         </Border> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter>  
     </Style> 

GroupStyle가 읽어 내 전용 불구하고, 우리는 당신이 MenuItem의 스타일을 조정할 수 있습니다 ContexMenuMenuItem 정확히 느낌을 얻기 위해 PropertyElement :-)

를 통해 설정할 수 있습니다

+0

리스트 박스 스타일 대신 메뉴의리스트 박스를 설정할 수 있습니다. 이 솔루션은 다른 스타일에도 적합해야합니다. – yossharel

0

나는 이것이 바인딩 DefaultGroupStyle 추가 ListBox 컨트롤에서 상속 새로운 컨트롤 만들이었다 해결 방법 :

public class MyListBox : ListBox 
    { 
     public GroupStyle DefaultGroupStyle 
     { 
      get { return (GroupStyle)GetValue(DefaultGroupStyleProperty); } 
      set { SetValue(DefaultGroupStyleProperty, value); } 
     } 

     // Using a DependencyProperty as the backing store for DefaultGroupStyle. This enables animation, styling, binding, etc... 
     public static readonly DependencyProperty DefaultGroupStyleProperty = 
      DependencyProperty.Register("DefaultGroupStyle", typeof(GroupStyle), typeof(MyListBox), new UIPropertyMetadata(null, DefaultGroupStyleChanged)); 

     private static void DefaultGroupStyleChanged(DependencyObject o, DependencyPropertyChangedEventArgs e) 
     { 
      ((MyListBox)o).SetDefaultGroupStyle(e.NewValue as GroupStyle); 
     } 

     private void SetDefaultGroupStyle(GroupStyle defaultStyle) 
     { 
      if (defaultStyle == null) 
      { 
       return; 
      } 

      if (this.GroupStyle.Count == 0) 
      { 
       this.GroupStyle.Add(defaultStyle); 
      } 
     } 
    } 
3

당신은 그룹 스타일을 추가 단순화하기 위해 연결된 속성을 사용할 수 있습니다

<Style TargetType="MenuItem"> 
    <Setter Property="b:GroupStyleEx.Append"> 
     <Setter.Value> 
      <GroupStyle .. /> 
     </Setter.Value> 
    </Setter> 

    <!-- you can add as many as you like... --> 
    <Setter Property="b:GroupStyleEx.Append"> 
     <Setter.Value> 
      <!-- second group style --> 
      <GroupStyle .. /> 
     </Setter.Value> 
    </Setter> 
</Style> 

여기에 연결된 속성에 대한 코드입니다 :

using System; 
using System.Windows; 
using System.Windows.Controls; 

namespace Util 
{ 
    public static class GroupStyleEx 
    { 
     public static readonly DependencyProperty AppendProperty 
      = DependencyProperty.RegisterAttached("Append", typeof (GroupStyle), typeof (GroupStyleEx), 
                new PropertyMetadata(AppendChanged)); 

     public static GroupStyle GetAppend(DependencyObject obj) 
     { 
      return (GroupStyle) obj.GetValue(AppendProperty); 
     } 

     public static void SetAppend(DependencyObject obj, GroupStyle style) 
     { 
      obj.SetValue(AppendProperty, style); 
     } 

     private static void AppendChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
     { 
      var itemsControl = d as ItemsControl; 
      if (itemsControl == null) 
       throw new InvalidOperationException("Can only add GroupStyle to ItemsControl"); 

      var @new = e.NewValue as GroupStyle; 
      if (@new != null) 
       itemsControl.GroupStyle.Add(@new); 
     } 
    } 
} 
관련 문제