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>

这种写法有提示:

相关推荐
dalong107 小时前
WPF:3D正四面体旋转改错
3d·wpf
dalong101 天前
WPF:3D正四面体自动旋转
3d·wpf
He BianGu1 天前
【WPF-Control】二次开发总览
wpf
绿浪19841 天前
WPF 后台刷新界面总结
wpf
绿浪19842 天前
WPF UI标准流水线
ui·wpf
心平气和量大福大2 天前
C#-WPF-控件-LiveChart图表-线性2(LineSeries)-数据绑定
开发语言·c#·wpf
绿浪19842 天前
WPF 防重入,时序错乱详解
wpf
whn19773 天前
oracle的节点2无法启动asm实例 提示PMON terminating the instance due to error 481
数据库·oracle·wpf
完美火龙篇 四月的友3 天前
WPF 笔迹延迟优化从硬件到软件的全链路分析
wpf
dalong103 天前
WPF:3D立方体
3d·wpf