如何绑定到这个字典条目?
How to bind to this dictionary entry?
我有一个 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 文本标签绑定到 Units 元素,如下所示:
<Label
Content="{Binding Source={x:Static c:Formatter.Units[Mass].Display}}"/>
它认为 Mass 是一个意想不到的标记, . 也是。
DataContext 未设置为 Formatter,而是设置为 ViewModel,顺便说一句。
问题:绑定必须是什么样的?
(XAML 应该显示 "kg"。)
XAML:
<Window.DataContext>
<local:MyViewModel/>
</Window.DataContext>
<Grid>
<Label x:Name="label1" Content="{Binding MyFormatter[Mass].Display}" Width="300" FontSize="18.667" Margin="95,10,123.4,232.8"/>
<Label x:Name="label2" Content="{Binding MyFormatter[MassPU].Display}" Width="300" FontSize="18.667" Margin="95,93,123.4,157.8"/>
</Grid>
视图模型:
public class MyViewModel
{
public Formatter MyFormatter { get; set; }
public MyViewModel()
{
MyFormatter = new Formatter();
}
}
格式化程序:
public class Formatter : Dictionary<string, Unit>
{
public Formatter()
{
Add("Mass", new Unit { Name = "Mass", Display = "Kg", Factor = 1 });
Add("MassPU", new Unit { Name = "MassPU", Display = "Kg/m", Factor = 0.001 });
}
}
单位:
public class Unit
{
public string Name { get; set; }
public string Display { get; set; }
public double Factor { get; set; }
}
我有一个 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 文本标签绑定到 Units 元素,如下所示:
<Label
Content="{Binding Source={x:Static c:Formatter.Units[Mass].Display}}"/>
它认为 Mass 是一个意想不到的标记, . 也是。
DataContext 未设置为 Formatter,而是设置为 ViewModel,顺便说一句。
问题:绑定必须是什么样的? (XAML 应该显示 "kg"。)
XAML:
<Window.DataContext>
<local:MyViewModel/>
</Window.DataContext>
<Grid>
<Label x:Name="label1" Content="{Binding MyFormatter[Mass].Display}" Width="300" FontSize="18.667" Margin="95,10,123.4,232.8"/>
<Label x:Name="label2" Content="{Binding MyFormatter[MassPU].Display}" Width="300" FontSize="18.667" Margin="95,93,123.4,157.8"/>
</Grid>
视图模型:
public class MyViewModel
{
public Formatter MyFormatter { get; set; }
public MyViewModel()
{
MyFormatter = new Formatter();
}
}
格式化程序:
public class Formatter : Dictionary<string, Unit>
{
public Formatter()
{
Add("Mass", new Unit { Name = "Mass", Display = "Kg", Factor = 1 });
Add("MassPU", new Unit { Name = "MassPU", Display = "Kg/m", Factor = 0.001 });
}
}
单位:
public class Unit
{
public string Name { get; set; }
public string Display { get; set; }
public double Factor { get; set; }
}