2012-03-07 10 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

<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> 

をここで添付プロパティのためのコードです。このソリューションは他のスタイルにも有効です – 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

あなたは、グループのスタイルを追加し簡素化するために、添付プロパティを使用することができます:あなたの代わりにメニューのリストボックスを設定することができListBoxのスタイルについては

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); 
     } 
    } 
} 
関連する問題