c# - 将标签内容绑定(bind)到嵌套类中的值而不是 Datacontext

标签 c# wpf mvvm binding mvvm-light

我的 View (InformationView) 绑定(bind)到 InformationViewModel 并且我使用嵌套类来维护当前的银行

我的嵌套类:

public class MainController : NotificationObject
{
    public MainController()
    {
        Initialize();
    }

    private void Initialize()
    {
        // TODO implement 
    }


    public static MainController Instance
    {
        get { return Nested.instance; }
    }

    private BankModel _currentBank;

    public BankModel CurrentBank
    {
        get { return _currentBank; }
        set
        {
            if (_currentBank== value)
            {
                return;
            }

            _currentBank= value;
            RaisePropertyChanged(() => CurrentBank);
        }
    }

    private class Nested
    {
        static Nested()
        {
        }

        internal static readonly MainController instance = new MainController();
    }
}

我的银行模型:
    private string _name ="test";

    public string Name
    {
        get
        {
            return _name;
        }

        set
        {
            if (_name == value)
            {
                return;
            }

            _name= value;
            RaisePropertyChanged(()=>Name);
        }
    }

我的 XAML
xmlns:Controller="clr-namespace:MyProject.Controller" 
/****/
<Label Content="{Binding Controller:MainController.CurrentBank.Name}"/>

首先我在我的标签中看不到“测试”,如果我执行我更改这个值并且我的标签总是为空,我如何用正确的方法做到这一点

最佳答案

您需要在绑定(bind)声明中使用“路径”和“源”的组合。您还需要提醒绑定(bind)引擎您正在访问静态成员。

<Label Content="{Binding Source={x:Static Controller:MainController.Instance}, Path=CurrentBank.Name}" />

关于c# - 将标签内容绑定(bind)到嵌套类中的值而不是 Datacontext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14753921/

相关文章:

javascript - JSON (node/express) 和 JSON 数组中的自定义 "types"

c# - 按钮上的文本在更改 IsEnabled 时失去中心对齐

wpf - 如何将 XAML 绑定(bind)延迟到加载后

WPF:简单的文本框数据绑定(bind)

c# - 为什么异步等待任务中的同步代码比异步代码慢得多

WPF MVVM : Add item not present in combobox

c# - 在 View 模型中创建控件实例

c# - 返回 bool 的空传播运算符

c# - asp.net 回发超时

c# - 如何在 WPF 中使用 MVVM 从另一个 View 打开一个 View