wpf将静态变量绑定到控件属性

有时候需要将后台一个静态属性绑定到xaml前台,经过实践,有如下两种绑定的方式

例如后台声明一个类,类中包含静态变量:

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赋值(不推荐)

<Button Margin="5" Height="30" Width="100" Content="{Binding Path=(local:Const.Name)}"></Button>

但是这种写法没有提示:

在这里插入图片描述

Binding的Source赋值

<Button Margin="5" Height="30" Width="100" Content="{Binding Source={x:Static local:Const.Name}}"></Button>

这种写法有提示:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/lishuangquan1987/article/details/134973499
今日推荐