数据绑定与WPF中的静态属性 [英] Data binding with static properties in WPF

查看:1930
本文介绍了数据绑定与WPF中的静态属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的静态属性 FontSizeTitle ,它应该用于 HandledWindow 类型,并从同一个静态属性更新,更改属性后,不显式通知。通过设置面板或任何将更改属性的内容,以便可视化地更改所有窗口的所有标题的和更新字体大小。

I have a simple static property FontSizeTitle which is supposed to be used for the stylised title in all instances of HandledWindow type, and update from the same static property at the same time without explicit notification after changing the property. by the settings panel or whatever that will change the property in order to change and update font sizes for all titles for all windows visually.

这里是XAML中我的风格化标题的代码,它是 HandledWindow 的模板的一部分,它是标准XAML样式页面的一部分,它是由资源字典从启动时的另一个图书馆。所以它适用于将出现在应用程序中的所有 HandledWindow 实例:

Here is the code of my stylised title in XAML, which is part of a template for the HandledWindow, which is part of a standard XAML style page, which is loaded by a resource dictionary from another library at startup. so it applies for all HandledWindow instances that will appear in the application:

<TextBlock x:Name="TitleText"
       TextWrapping="Wrap"
       Text="Window Title"
       FontSize="{Binding Source={x:Static UI:HandledWindow.FontSizeTitle}, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"
       VerticalAlignment="Stretch"
       FontFamily="{DynamicResource FontFamiliy}" />

这是我的简单静态属性,请注意,绑定实际上仅在第一次工作。 >

Here is my simple static property, Note that binding actually works for the first time only.

public static double FontSizeTitle
{
    get;
    set;
}

HandledWindow 键入它设置为15,该大小正在工作,但如果在初始化后再次设置为另一个大小,则视觉标题将不会更新。

By the base constructor of HandledWindow type it is set to 15, That size is working, But if setting it once again to another size after the initialisation the visual title won't update.

推荐答案

不确定,你正在使用什么版本的WPF。 WPF 4.5现在支持静态属性的绑定和属性更改通知。

Not sure, what version of WPF you are using. WPF 4.5 now supports Binding and Property Change notifications for Static Properties.

请参阅本博客 post 进行全面讨论。

Refer to this Blog post for full discussion.

所以,你的HandledWindows类将变成如下:

So, your HandledWindows class will become something like:

public static class HandledWindow
{
    private static double _fontSizeTitle;

    public static event EventHandler<PropertyChangedEventArgs> StaticPropertyChanged;

    static HandledWindow()
    {
        FontSizeTitle = 15;
    }

    public static double FontSizeTitle
    {
        get { return _fontSizeTitle; }
        set
        {
            _fontSizeTitle = value;
            if (StaticPropertyChanged != null)
                StaticPropertyChanged(null, new PropertyChangedEventArgs("FontSizeTitle"));
        }
    }
}

而XAML中的绑定将成为:

And the Binding in XAML will become:

FontSize="{Binding Path=(local:HandledWindow.FontSizeTitle), Mode=OneWay}"

这篇关于数据绑定与WPF中的静态属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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