1

I have an issue with defining FontFamily to my textboxes applicationwide, because my font family is defined at window level with this style :

<Style TargetType="Window" >
    <Setter Property="FontFamily" Value="Lucida Sans Unicode"/>
    <Setter Property="FontSize" Value="10pt"/>
</Style>

I have defined the font-family for my textboxes this way :

<Style TargetType="{x:Type TextBox}">
    <Setter Property="FontFamily" Value="Arial"/>
    <Setter Property="FontSize" Value="10pt"/>
</Style>

However, my TextBox style is not applied, and the text into the textboxes is still Lucida, and not Arial. How can I do that ? Is there a css-like !important equivalent in XAML style to override previous one ?

I notice that I really appreciate a XAML way to perform it on ResourceDictionaries.

Thanks for answers

cdie
  • 4,014
  • 4
  • 34
  • 57
  • This turns out to be kind of interesting. Can you confirm that you're setting those Styles in App.xaml, rather than in each Window? – goobering Jun 26 '15 at 10:33
  • Neither in App.xaml and Window, I defined them into a ResourceDictionary that is dynamcally loaded at runtime – cdie Jun 26 '15 at 10:38
  • It looks a lot like there's something weird (that I don't entirely understand) going on with Window/Control level settings for the entire application. See Gishu's answer here: http://stackoverflow.com/questions/431940/how-to-set-default-wpf-window-style-in-app-xaml , and Nicolas's here: http://stackoverflow.com/questions/4279773/wpf-window-style-not-being-applied . – goobering Jun 26 '15 at 10:48

1 Answers1

-1

It is working for me. I defined two styles, one for window, one special for text box. Special style wins and result is Arial 30pt. If I comment special style then window style is applied for textBox and comic sans 20pt appear.

See code:

<Window x:Class="WpfApplication3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication3"
        Title="MainWindow" Height="350" Width="525">
  <Grid>
    <TextBox Text="Hello!"/>
  </Grid>
</Window>

Application resources:

<Application x:Class="WpfApplication3.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
    <Style TargetType="Window" >
      <Setter Property="FontFamily" Value="Comic Sans MS"/>
      <Setter Property="FontSize" Value="20pt"/>
    </Style>
    <Style TargetType="{x:Type TextBox}">
      <Setter Property="FontFamily" Value="Arial"/>
      <Setter Property="FontSize" Value="30pt"/>
    </Style>
  </Application.Resources>
</Application>
Peter Dub
  • 491
  • 5
  • 12
  • This looked entirely sensible to me until I tried to run it. I get the right result in design mode, but not during debugging. – goobering Jun 26 '15 at 10:04
  • In fact, defining the style with the key make the solution works, so I'll do that just because all my window inheritates of one – cdie Jun 26 '15 at 10:52