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>

这种写法有提示:

相关推荐
samble3 天前
从零搭建灌装监控系统(四):深色主题与样式系统,ResourceDictionary 统一管理
c#·wpf·mvvm·样式·工业控制
_Jonas3 天前
.net framework开发环境正式环境配置文件的切换
.net·wpf
dalong105 天前
WPF:Modbus 状态监控
wpf·modbus
He BianGu5 天前
【WPF-VisionMaster】机器视觉通用平台V5.0版本发行说明
opencv·c#·wpf·halcon·机器视觉·visionmaster
fogota9 天前
Emoji与Segoe MDL2 Assets的比较
c#·wpf
fogota9 天前
C# WPF 按钮加载字体图标 / 动物头像
c#·.net·wpf
fogota9 天前
C# WPF 按钮加载系统图标 / 动物头像
c#·.net·wpf
军训猫猫头9 天前
C# 封装 MySQL 自动写入与查询工具类(基于 MySqlConnector)
mysql·c#·.net·wpf
gis开发之家10 天前
Spring Boot 4 定时任务与异步线程池:从 @Scheduled 到高并发任务编排(生产级实战)
java·spring boot·后端·wpf·spring boot4
clz131452111 天前
如何设计一个优雅可靠的金融级联机接口
wpf