1

I have the following scenario:

<UserControl.Resources>
    <Style x:Key="NormalFontStyle">
        <Setter Property="Control.FontFamily" Value="{Binding MyFont}"></Setter>
    </Style>
    <Style x:Key="BigFontStyle">
        <Setter Property="Control.FontFamily" Value="{Binding MyFont}"></Setter>
        <Setter Property="Control.FontSize" Value="{Binding MyBigFontSize}"></Setter>
    </Style>
</UserControl.Resources>

<Grid Style="{StaticResource NormalFontStyle}">
    <!-- Grid Contents -->
</Grid>

The DataContext of the Grid is the ViewModel containing MyFont and MyBigFontSize properties. The above code works properly, and every text inside the grid has "NormalFontStyle" applied.

Now the tricky part: I want to apply the "BigFontStyle" to a control inside the grid that may or may not have the same DataContext, which means that i cannot use this approach. Maybe binding the Values of the setters to static properties is the only way to go, (i just found this workaround for 3.5, which is my case here) but any light on this is welcome.

Community
  • 1
  • 1
Natxo
  • 2,917
  • 1
  • 24
  • 36

1 Answers1

2

You should put you properties into a singleton, this way you can bind to and edit them from anywhere in your application.

MySingleton.cs (ViewModelBase contains an implementation of INotifyPropertyChanged)

public class MySingleton: ViewModelBase
{
    private static MySingleton instance;
    private static readonly object padlock = new object();

    private FontFamily _myFont = new FontFamily();

    public static MySingleton Instance
    {
        get
        {
            lock (padlock)
            {
                if (instance == null)
                {
                    instance = new MySingleton();
                }
            }
            return instance;
        }
    }

    public FontFamily MyFont
    {
        get { return _myFont ; }
        set
        {
            _myFont = value;
            OnPropertyChanged("MyFont");
        }
    }
}

App.xaml

<Application ...
             xmlns:local="clr-namespace:ScrumManagementClient.ViewModel">
    <Application.Resources>
        <ResourceDictionary>
            <local:CurrentDataSingleton x:Key="Singleton"/>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="MyResourceDictionary.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

MyResourceDictionary.xaml

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

        <Style x:Key="NormalFontStyle">
            <Setter Property="Control.FontFamily" Value="{Binding Source={StaticResource  Singleton}, Path=Instance.MyFont}"/>
        </Style>

        <Style x:Key="BigFontStyle">
            <Setter Property="Control.FontFamily" Value="{Binding MyFont}"/>
            <Setter Property="Control.FontSize" Value="{Binding MyBigFontSize}"/>
        </Style>
    <ResourceDictionary/>

Now you can use to your stlyes from anywhere in your application:

`Style="{StaticResource stylename}"`

And to set a value in c# use:

MySingleton.Instance.Property = ?
Eamonn McEvoy
  • 8,876
  • 14
  • 53
  • 83
  • There is no 'BigFontStyle' property. I'm not sure what you're trying to do, but this won´t help. – Natxo Nov 10 '11 at 11:49
  • Notice that the binding to the properties will be made in the DataContext of the control you are applying the style to, and you may have controls with different DataContexts. This is what i need to solve. These solutions are ok if you are always using the styles on a DataContext in which 'MyFont' and 'MyBigFontSize' exist. – Natxo Nov 10 '11 at 13:03
  • 1
    @Natxo o sorry, I understand your problem now, I think you will need to move these properties out of the DataContext and into a singleton. Then you will be able to change them from different locations and bind to them in a ResourceDictionary. http://www.dreamincode.net/forums/topic/122587-wpf-two-way-binding-to-a-static-property/ – Eamonn McEvoy Nov 10 '11 at 16:06
  • Thanks for the effort Eammonn, maybe i didn't explain myself clearly enough. And yes, i thought i'll be ending up doing something like that but i wanted to see some other posible solutions. Feel free to modify your answer again. – Natxo Nov 10 '11 at 17:36
  • @Natxo I have posted a new answer, i think it should work for you – Eamonn McEvoy Nov 10 '11 at 18:03