【问题标题】:Binding a Style Setter Value properly正确绑定样式设置器值
【发布时间】:2011-11-11 02:02:02
【问题描述】:

我有以下场景:

<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 的 DataContext 是包含 MyFont 和 MyBigFontSize 属性的 ViewModel。上面的代码工作正常,网格内的每个文本都应用了“NormalFontStyle”。

现在棘手的部分是:我想将“BigFontStyle”应用于网格内可能具有或不具有相同 DataContext 的控件,这意味着我不能使用这种方法。 也许将设置器的值绑定到静态属性是唯一的方法,(我刚刚找到了 3.5 的 this 解决方法,这是我的情况),但欢迎对此提出任何启示。

【问题讨论】:

    标签: .net wpf xaml wpf-controls styles


    【解决方案1】:

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

    MySingleton.cs(ViewModelBase 包含 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/>
    

    现在您可以在应用程序的任何位置使用您的样式:

    `Style="{StaticResource stylename}"`
    

    并在 c# 中设置一个值:

    MySingleton.Instance.Property = ?
    

    【讨论】:

    • 没有“BigFontStyle”属性。我不确定您要做什么,但这无济于事。
    • 请注意,与属性的绑定将在您应用样式的控件的 DataContext 中进行,并且您可能拥有具有不同 DataContexts 的控件。这是我需要解决的。如果您始终在存在 'MyFont' 和 'MyBigFontSize' 的 DataContext 上使用样式,这些解决方案是可以的。
    • @Natxo 抱歉,我现在了解您的问题,我认为您需要将这些属性移出 DataContext 并放入单例中。然后,您将能够从不同的位置更改它们并在 ResourceDictionary 中绑定到它们。 dreamincode.net/forums/topic/…
    • 感谢 Eammonn 的努力,也许我解释得不够清楚。是的,我认为我最终会做类似的事情,但我想看看其他一些可能的解决方案。请随时再次修改您的答案。
    • @Natxo 我发布了一个新答案,我认为它应该适合你
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-24
    • 2017-01-30
    • 1970-01-01
    • 2021-10-24
    • 2014-02-27
    相关资源
    最近更新 更多