1
我有一个XML文件,其中包含,除其他事项:如何绑定到这个字典条目?
<Unit Name="Mass" Factor="1" Display="kg"/>
<Unit Name="MassPU" Factor="0.001" Display="kg/m"/>
该数据被读入到这样的字典:
Formatter.Units = (data.Descendants("Unit").Select(x => new Unit
(
x.Attribute("Name").Value,
x.Attribute("Display").Value,
Convert.ToDouble(x.Attribute("Factor").Value)
)
) ToList()).ToDictionary(x => x.Name, x => x);
然后我有这样的C#代码(除其他东西它):
namespace mySpace
{
public static class Formatter
{
public enum MUnits {Mass = 0, Force, .... etc };
public static Dictionary<string, Unit> Units { get; set; }
}
}
现在我需要一个XAML文本标签绑定到一个单位元素是这样的:
<Label
Content="{Binding Source={x:Static c:Formatter.Units[Mass].Display}}"/>
它认为质量是一个意外的令牌,和。。
DataContext未设置为格式化程序而是ViewModel,btw。
问题:绑定看起来像什么? (该XAML应显示 “公斤”。)
为什么不创建ObservableCollection的单位?字典根本不适合绑定。 – Spawn
我会使用ValueConverter来获取字典元素 – thumbmunkeys