WPF ::将RibbonComboBox设置为与RibbonGallery不同的样式

时间:2015-04-18 18:37:48

标签: wpf ribbon ribbon-control ribboncontrolslibrary windows-ribbon-framework

我有RibbonComboBox用于设置字体大小。它有RibbonGallery列出了各种字体大小,显示在相应的FontSize

<r:RibbonComboBox DataContext="{x:Static vm:RibbonDataModel.FontSizeComboBoxData}"
                  SelectionBoxWidth="30">
   <r:RibbonGallery MaxColumnCount="1"
                    Command="{Binding Command}"
                    CommandParameter="{Binding SelectedItem}">
      <r:RibbonGallery.GalleryItemTemplate>
         <DataTemplate>
            <Grid>
               <TextBlock Text="{Binding}"
                          FontSize="{Binding}" />
            </Grid>
         </DataTemplate>
      </r:RibbonGallery.GalleryItemTemplate>
   </r:RibbonGallery>
</r:RibbonComboBox>

编辑这是我的ViewModel:

public static RibbonDataModel
{
  public static GalleryData<object> FontSizeComboBoxData
  {
     get
     {
        lock (LockObject)
        {
           const string key = "Font Size";
           if (!DataCollection.ContainsKey(key))
           {
              var value = new GalleryData<object>
              {
                 Command = HtmlDocumentCommands.ChangeFontSize,
                 Label = "Change Font Size",
                 ToolTipDescription = "Set the font to a specific size.",
                 ToolTipTitle = "Change Font Size",
              };

              var fontSizes = new GalleryCategoryData<object>();
              var i = 9.0;
              while (i <= 30)
              {
                 fontSizes.GalleryItemDataCollection.Add(i);
                 i += 0.75;
              }
              value.CategoryDataCollection.Add(fontSizes);
              DataCollection[key] = value;
           }
           return DataCollection[key] as GalleryData<object>;
        }
     }
  }
}

所有内容都按预期工作,但在我从图库中选择一个项目后,它会在RibbonComboBox中显示与在图库中使用的相同的巨大(或微小)FontSize

我怎样才能重置&#34;所选项目的FontSize默认显示在RibbonComboBox

2 个答案:

答案 0 :(得分:5)

RibbonComboBox使用ContentPresenter来显示您在RibbonGallery中选择的项目。 此外,ContentPresenter采用您在ItemTemplate中声明的RibbonGallery。 这是&#34;核心&#34;你的问题的原因。

因此,您可以选择两种解决方案来解决问题。

第一个解决方案(最快的解决方案)

您可以简单地将RibbonComboBox的IsEditable属性设置为&#34; true&#34;。通过这种方式,RibbonComboBox用TextBox替换ContentPresenter,而不使用任何ItemTemplate。然后字体将具有正确的大小。

第二个解决方案(最好的一个恕我直言)

由于在RibbonComboBox的ContentPresenter和RibbonGallery中同时使用了ItemTemplate,因此我们可以尝试解决问题。 olny的区别在于,当DataTemplate放置在RibbonGallery中时,其父级是RibbonGalleryItem。 因此,如果其父级不是RibbonGalleryItem,则会自动知道DataTemplate被放置在ContentPresenter中。 您可以通过编写简单的DataTrigger来处理这种情况。 让我们在代码中看到所有内容。

我写了一个简化的ViewModel:

namespace WpfApplication1
{
    public class FontSizes
    {
        private static FontSizes instance = new FontSizes();
        private List<double> values = new List<double>();

        public FontSizes()
        {
            double i = 9.0;
            while (i <= 30)
            {
                values.Add(i);
                i += 0.75;
            }
        }

        public IList<double> Values
        {
            get
            {
                return values;
            }
        }

        public static FontSizes Instance
        {
            get
            {
                return instance;
            }
        }
    }
}

然后这是我的观点:

<Window x:Class="WpfApplication1.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:ribbon="http://schemas.microsoft.com/winfx/2006/xaml/presentation/ribbon"
        xmlns:vm="clr-namespace:WpfApplication1"
        Title="Window1" Height="300" Width="300">
    <Window.Resources />

    <DockPanel>
        <ribbon:RibbonComboBox Label="Select a font size:"
                  SelectionBoxWidth="62"
                  VerticalAlignment="Center">

        <ribbon:RibbonGallery MaxColumnCount="1">
                <ribbon:RibbonGalleryCategory DataContext="{x:Static vm:FontSizes.Instance}" ItemsSource="{Binding Path=Values, Mode=OneWay}">
                    <ribbon:RibbonGalleryCategory.ItemTemplate>
                        <DataTemplate>
                            <Grid>
                                <TextBlock Name="tb" Text="{Binding}" FontSize="{Binding}" />
                            </Grid>

                            <DataTemplate.Triggers>
                                <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ribbon:RibbonGalleryItem, AncestorLevel=1}}"
                                             Value="{x:Null}">
                                    <Setter TargetName="tb" Property="FontSize" Value="12" />
                                </DataTrigger>
                            </DataTemplate.Triggers>
                        </DataTemplate>
                    </ribbon:RibbonGalleryCategory.ItemTemplate>
                </ribbon:RibbonGalleryCategory>
            </ribbon:RibbonGallery>
        </ribbon:RibbonComboBox>
    </DockPanel>
</Window>

正如您所看到的,DataTrigger是&#34;组件&#34;这使得#34;脏工作&#34;。

现在,您只需要考虑自己喜欢哪种解决方案。

答案 1 :(得分:1)

我建议你使用Fluent.Ribbon库而不是Microsoft Ribbons(因为它们非常错误,维护得不好而且只支持旧样式,真的相信我这个它会给你带来很多麻烦)。

然后你就可以使用这段代码:

<fluent:ComboBox Header="Font Size" ItemsSource="{Binding FontSizes}">
    <fluent:ComboBox.ItemTemplate>
        <ItemContainerTemplate>
            <TextBlock FontSize="{Binding }" Text="{Binding }" />
        </ItemContainerTemplate>
    </fluent:ComboBox.ItemTemplate>
</fluent:ComboBox>

获得理想的结果:

enter image description here