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>

这种写法有提示:

相关推荐
甄天9 天前
WPF中MVVM和MVVMLight模式
c#·wpf·visual studio
CoderIsArt11 天前
C# WPF常用调试工具汇总
开发语言·c#·wpf
凉、介12 天前
SylixOS 下的消息队列
linux·wpf·sylixos
Magnum Lehar12 天前
wpf3d游戏引擎ProjectLayoutView实现
游戏引擎·wpf
摆烂的少年12 天前
WPF中自定义DataGrid表格后,修改选中行的字体颜色或背景图
wpf
CoderIsArt12 天前
WPF调试三种工具介绍:Live Visual Tree、Live Property Explorer与Snoop
wpf
甄天12 天前
WPF Style样式 全局样式资源字典
c#·wpf
GalaxyPokemon13 天前
RPC - 服务注册与发现模块
wpf
FuckPatience13 天前
WPF 的RenderTransform使图标旋转180°
wpf
甄天13 天前
WPF数据绑定
c#·wpf