WebEnh

.net7 mvc jquery bootstrap json 学习中 第一次学PHP,正在研究中。自学进行时... ... 我的博客 https://enhweb.github.io/ 不错的皮肤:darkgreentrip,iMetro_HD
随笔 - 1079, 文章 - 1, 评论 - 75, 阅读 - 174万
  首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8

WPF 中具有静态属性的数据绑定

Posted on   WebEnh  阅读(299)  评论(0编辑  收藏  举报

我有一个简单的静态属性FontSizeTitle,它应该用于所有HandledWindow类型实例中的风格化标题,并在更改属性后同时从同一个静态属性更新而无需明确通知。通过设置面板或任何会更改属性的内容,以便直观地更改和更新所有窗口的所有标题的字体大小。

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

<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}" />
Run Code Online (Sandbox Code Playgroud)

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

public static double FontSizeTitle
{
    get;
    set;
}
Run Code Online (Sandbox Code Playgroud)

通过HandledWindow类型的基本构造函数将其设置为 15,该大小有效,但是如果在初始化后再次将其设置为另一个大小,则视觉标题不会更新。

 

sth*_*ura  6

 

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

有关完整讨论,请参阅此博客文章

因此,您的 HandledWindows 类将变成:

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;
           StaticPropertyChanged?.Invoke(null, new PropertyChangedEventArgs(nameof(FontSizeTitle)));
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

XAML 中的 Binding 将变为:

FontSize="{Binding Path=(local:HandledWindow.FontSizeTitle), Mode=OneWay}"
Run Code Online (Sandbox Code Playgroud)

 

 
相关博文:
阅读排行:
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
历史上的今天:
2019-05-27 cshtml 中的 AppState = Context.Application 和 控制器中的 Application 也相等
点击右上角即可分享
微信分享提示

喜欢请打赏

扫描二维码打赏

了解更多