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>

这种写法有提示:

相关推荐
c#上位机8 小时前
wpf附加事件
wpf
玖笙&8 小时前
✨WPF编程进阶【9.1】:WPF资源完全指南(附源码)
c++·c#·wpf·visual studio
想你依然心痛8 小时前
HarmonyOS 6(API 23)分布式实战:基于悬浮导航与沉浸光感的“光影协创“跨设备白板系统
分布式·wpf·harmonyos·悬浮导航·沉浸光感
c#上位机2 天前
wpf路由事件
wpf
nashane2 天前
HarmonyOS 鸿蒙 2026 全栈实战:从手势驱动到分布式数据落地的完整架构
wpf·harmony app
秋雨雁南飞2 天前
WPF 国际化(全球化)管理
wpf
nashane3 天前
HarmonyOS 6.0 分布式数据库进阶:设备协同与高效数据同步实战(API 11 Stage 模型)
wpf·harmonyos 5
极客智造3 天前
WPF InputBindings MVVM详解
wpf
nashane3 天前
HarmonyOS 6.0 分布式数据实战:KVStore跨设备同步与高性能查询指南(API 11 Stage模型)
wpf·harmonyos 5
SEO-狼术4 天前
Easily Reorder Rows in WPF Grids
wpf