wpf将静态变量绑定到控件属性

有时候需要将后台一个静态属性绑定到xaml前台,经过实践,有如下两种绑定的方式

例如后台声明一个类,类中包含静态变量:

csharp 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 静态变量绑定
{
    public class Const
    {
        public static string Name { get; set; } = "Tony";
    }
}

Binding的Path赋值(不推荐)

xml 复制代码
<Button Margin="5" Height="30" Width="100" Content="{Binding Path=(local:Const.Name)}"></Button>

但是这种写法没有提示:

Binding的Source赋值

xml 复制代码
<Button Margin="5" Height="30" Width="100" Content="{Binding Source={x:Static local:Const.Name}}"></Button>

这种写法有提示:

相关推荐
△曉風殘月〆17 小时前
WPF Prism中的MVVM实现
wpf·mvvm
量子物理学17 小时前
.NET8 中 WPF与ScottPlot 报表 的完美结合
.net·wpf
△曉風殘月〆17 小时前
WPF Prism区域导航功能详解
wpf·mvvm
星河Cynthia2 天前
WPF基于resx资源文件的多语言实现
c#·wpf
量子物理学2 天前
WPF 标签预览可以显示图片运行后不显示
c#·wpf
△曉風殘月〆2 天前
WPF Prism中的依赖注入详解
wpf·mvvm
△曉風殘月〆2 天前
WPF Prism创建Bootstrapper/启动器
wpf·mvvm
小曹要微笑2 天前
WPF的依赖与附加属性
wpf·依赖属性·附加属性
bugcome_com3 天前
WPF 命令 ICommand 从原理到实战
后端·wpf·icommand
武藤一雄3 天前
WPF处理耗时操作的7种方法
microsoft·c#·.net·wpf