English 中文(简体)
装饰 价值
原标题:Binding a Style Setter Value properly

我有以下设想:

<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>

Grid的数据背景是含有MyFont和MyBigFontSize财产的《观点》。 上述法规是适当的,电网内的每一文本都适用“NormalFontStyle”。

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.

最佳回答

你们应该把你们的财产放在一个单一州,这样,你就可以在你的申请中把财产从任何地方约束起来。

MySingleton.cs(ModelBase 诉加拿大)

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/>

现在,你可以在你申请时从任何地点用 st子:

`Style="{StaticResource stylename}"`

And to set a value in c# use:

MySingleton.Instance.Property = ?
问题回答

暂无回答




相关问题
Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How do I compare two decimals to 10 decimal places?

I m using decimal type (.net), and I want to see if two numbers are equal. But I only want to be accurate to 10 decimal places. For example take these three numbers. I want them all to be equal. 0....

Exception practices when creating a SynchronizationContext?

I m creating an STA version of the SynchronizationContext for use in Windows Workflow 4.0. I m wondering what to do about exceptions when Post-ing callbacks. The SynchronizationContext can be used ...

Show running instance in single instance application

I am building an application with C#. I managed to turn this into a single instance application by checking if the same process is already running. Process[] pname = Process.GetProcessesByName("...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签