gpt4 book ai didi

wpf - Silverlight 绑定(bind)到字典中的项目

转载 作者:行者123 更新时间:2023-12-03 10:30:28 24 4
gpt4 key购买 nike

如果我将 ViewModel 定义为以下内容:

public class MainViewModel : DynamicObject
{
public Dictionary<string, string> Attributes { get; set; }
public MainViewModel()
{
Attributes = new Dictionary<string, string>();
Attributes["Welcome"] = "Welcome to MVVM Light";
}

public override bool TryGetMember(GetMemberBinder binder, out object result)
{
if (Attributes.ContainsKey(binder.Name))
{
result = Attributes[binder.Name];
}
else
result = "";
return true;
}
}

在silverlight中,我收到以下错误:
System.Windows.Data Error: BindingExpression path error: 'Welcome' property not found on 'DictionaryBasedVM.ViewModel.MainViewModel' 'DictionaryBasedVM.ViewModel.MainViewModel' (HashCode=30542218). BindingExpression: Path='Welcome' DataItem='DictionaryBasedVM.ViewModel.MainViewModel' (HashCode=30542218); target element is 'System.Windows.Controls.TextBlock' (Name=''); target property is 'Text' (type 'System.String')..

在 WPF 中同样可以正常工作。

最佳答案

试试这个“{Binding Attributes[Welcome]}”

关于wpf - Silverlight 绑定(bind)到字典中的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5949826/

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