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>

这种写法有提示:

相关推荐
aGdF8E3gQ1 天前
【Application Insights】采样率对Function App日志收集的影响和解决方法
python·flask·wpf
程序员-Benothing1 天前
如何实现高并发系统订单30分钟自动关闭?
开发语言·c#·wpf
一水2 天前
Redis实战:一个AI工作流系统里的五个应用场景,从传参到限流的完整链路
数据库·redis·wpf
dalong102 天前
WPF:3D甜甜圈
3d·c#·wpf·甜甜圈
别催小唐敲代码2 天前
ROS2从入门到实战:去中心化机器人操作系统详解
wpf·ros2
脚踏实地皮皮晨3 天前
003006001_WrapPanel控件
开发语言·c#·.net·wpf·visual studio
脚踏实地皮皮晨3 天前
003005002_WPF StackPanel 基类官方类定义逐行深度解析
开发语言·windows·算法·c#·wpf·visual studio
脚踏实地皮皮晨3 天前
003004002_UniformGrid控件的使用方法
开发语言·c#·.net·wpf·visual studio
脚踏实地皮皮晨4 天前
003003002_WPF Grid 基类官方类定义逐行深度解析
开发语言·windows·算法·c#·wpf·visual studio
脚踏实地皮皮晨5 天前
003002004_WPF Panel 基类 官方类定义
开发语言·windows·算法·c#·wpf·visual studio