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

事实上,一些额外的工作,这是可以做到:
而不是ContexMenu模板设置为ItemsPresenter,你可以将其设置为控制,将适合你的数据。在这种情况下,您可以将其设置为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是只读的,我们可以通过PropertyElement :-)

将其设置为获得的ContexMenuMenuItem准确的感觉,你可以调整的MenuItem风格

+0

对于ListBox的风格,你可以设置的,而不是菜单列表框。这个解决方案也适用于其他样式 – yossharel 2012-03-11 14:14:10

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); 
     } 
    } 
}