在带有MVVM的RibbonControlsLibrary中使用GroupSizeReductionOrder

在带有MVVM的RibbonControlsLibrary中使用GroupSizeReductionOrder

问题描述:

我正在为我的色带使用RibbonControlsLibrary(.Net 4.0)。我的实现是使用MVVM完全实现的。 现在我喜欢使用调整大小功能的好处。但我无法设置组框的缩减顺序。这是因为我必须按特殊顺序定义组框的名称。我无法设置组框的名称,因为我正在使用数据模板。 我试图将功能区组框用户控件的名称绑定到datacontext上的一个属性,但是我认为这不起作用。在带有MVVM的RibbonControlsLibrary中使用GroupSizeReductionOrder

那么,我有没有机会将功能区组框用户控件名称设置为数据上下文的属性?

+1

你能粘贴源代码吗? – kmatyaszek 2012-07-18 17:04:44

设置RibbonGroups的名字

可能还有其他的解决方案,我的是实现一个Behavior,在OnAttached方法设置RibbonGroup的名字。

public class RibbonGroupNameBehavior : Behavior<RibbonGroup> 
{ 
    protected override void OnAttached() 
    { 
     base.OnAttached(); 

     GroupeCommandesViewModel groupeCommandesViewModel = this.AssociatedObject.DataContext as GroupeCommandesViewModel; 
     if (groupeCommandesViewModel != null) 
     { 
      this.AssociatedObject.Name = groupeCommandesViewModel.Nom; 
     } 
    } 
} 

您可能要添加分配周围try-catch块...

使用行为

当你正在使用的DataTemplates,我认为你必须要分配行为添加到带有样式的RibbonGroup。 我一直在使用this solution成功。 如您所见,我的行为代码不提供所需的依赖项属性,您必须添加它。

这里是我的缩短丝带的风格:

<r:Ribbon.Style> 
    <Style TargetType="{x:Type r:Ribbon}"> 
     <Setter Property="ItemContainerStyle"> 
      <Setter.Value> 
       <Style TargetType="{x:Type r:RibbonTab}"> 
        <Setter Property="ItemsSource" Value="{Binding ListeCommandes, Converter={StaticResource CommandesToGroupesConverter}}" /> 
        <Setter Property="GroupSizeReductionOrder" Value="{Binding ListeCommandesOrdreRedimensionnement}" /> 
        <Setter Property="ItemContainerStyleSelector"> 
         <Setter.Value> 
          <ribbonVM:RibbonGroupStyleSelector> 
           <ribbonVM:RibbonGroupStyleSelector.GenericStyle> 
            <Style TargetType="{x:Type r:RibbonGroup}" BasedOn="{StaticResource RibbonGroupStyle}" /> 
           </ribbonVM:RibbonGroupStyleSelector.GenericStyle> 
          </ribbonVM:RibbonGroupStyleSelector> 
         </Setter.Value> 
        </Setter> 
       </Style> 
      </Setter.Value> 
     </Setter> 
    </Style> 
</r:Ribbon.Style> 

而且我RibbonGroup的风格:

<Style x:Key="RibbonGroupStyle" TargetType="{x:Type r:RibbonGroup}" BasedOn="{StaticResource RibbonControlStyle}"> 
    <Setter Property="Header"  Value="{Binding Libellé}" /> 
    <Setter Property="ItemsSource" Value="{Binding Commandes}" /> 

    <Setter Property="CanAddToQuickAccessToolBarDirectly" Value="False" /> 

    <Setter Property="ihm_behaviors:RibbonGroupNameBehavior.IsEnabled" Value="True" /> 
</Style> 

数据绑定的GroupSizeReductionOrder财产

如果你看一下GroupSizeReductionOrder属性的定义,你将会看到它是一个带有TypeConverter StringCollectionConverter的StringCollection。 从我所看到的,这个转换器只能从字符串转换为StringCollection。 所以你的财产应该是一个字符串或一个StringCollection。