正确绑定样式设置器值 [英] Binding a Style Setter Value properly

查看:54
本文介绍了正确绑定样式设置器值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下情况:

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

网格的DataContext是包含MyFont和MyBigFontSize属性的ViewModel.上面的代码正常工作,并且网格内的每个文本都应用了"NormalFontStyle".

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.

现在棘手的部分:我想将"BigFontStyle"应用到可能具有或不具有相同DataContext的网格内的控件,这意味着我无法使用此方法.也许将设置方法的值绑定到静态属性是唯一的方法,(我刚刚发现 3.5的解决方法,这是我的例子),但欢迎对此轻描淡写.

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.

推荐答案

您应该将属性放入单例中,这样您就可以在应用程序中的任何位置绑定并编辑它们.

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包含INotifyPropertyChanged的实现)

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

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

现在,您可以在应用程序中的任何位置使用stly:

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

`Style="{StaticResource stylename}"`

要在c#中设置值,请使用:

And to set a value in c# use:

MySingleton.Instance.Property = ?

这篇关于正确绑定样式设置器值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆