The RibbonComboBox uses the ContentPresenter to display the item you selected in the RibbonGallery . In addition, ContentPresenter accepts the same ItemTemplate that you specified in RibbonGallery . This is the "root cause" of your problem.
Thus, you can choose one of two solutions to the problem.
FIRST SOLUTION (fastest)
You can simply set the IsEditable property of your RibbonComboBox to true. Thus, the RibbonComboBox replaces the ContentPresenter with a text field without using an ItemTemplate. Then the font will have the right size.
SECOND DECISION (best IMHO)
Since the ItemTemplate is used at the same time from both the RibbonComboBox ContentPresenter and the RibbonGallery, this is the moment when we can try to solve the problem. The difference in olny is that when a DataTemplate is placed inside the RibbonGallery, its parent is the RibbonGalleryItem . Therefore, if its parent is not a RibbonGalleryItem , you automatically know that the DataTemplate is inside the ContentPresenter. You can handle this situation by writing a simple DataTrigger . Let's see everything in the code.
I wrote a simplified 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; } } } }
Then this is my view:
<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>
As you can see, DataTrigger is a “component” that does the “dirty work”.
Now you just need to think about which solution you prefer.
Il vic
source share