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>

这种写法有提示:

相关推荐
oioihoii5 小时前
WPF入门指南:解析默认项目结构
wpf
极客智造8 小时前
深入解析 ReactiveUI:WPF 响应式 MVVM 开发的 “终极方案”
wpf
Macbethad2 天前
使用WPF编写一个多维度伺服系统的程序
大数据·hadoop·wpf
lingxiao168882 天前
WPF Prism框架应用
c#·wpf·prism
Macbethad2 天前
使用WPF编写一个Ethercat主站的程序
wpf
难搞靓仔3 天前
WPF 弹出窗体Popup
wpf·popup
Macbethad3 天前
使用WPF编写一个MODBUSTCP通信的程序
wpf
unicrom_深圳市由你创科技3 天前
Avalonia.WPF 跨平台图表的使用
wpf
-大头.3 天前
深入解析ZooKeeper核心机制
分布式·zookeeper·wpf
Macbethad3 天前
使用WPF编写一个RS232主站程序
wpf