gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 10:23:41 25 4
gpt4 key购买 nike

我的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com