问题
I have a Menuitem which is supposed to show a list of quantities as children which show a list of the units defined for this quantity as children. The list of quantities is set in code-behind to the ItemsSource of the MenuItem.
<MenuItem Header="common.unitsystem" Name="mnuItemUnits">
<MenuItem.Resources>
<DataTemplate DataType="{x:Type guidev:Measurement}">
<StackPanel>
<TextBlock Text="Measurement"/>
<TextBlock Text="{Binding Name}"/>
</StackPanel>
</DataTemplate>
<HierarchicalDataTemplate DataType="{x:Type guidev:Quantity}" ItemsSource="{Binding Measurements}">
<StackPanel>
<TextBlock Text="Quantity "/>
<TextBlock Text="{Binding Name}"/>
</StackPanel>
</HierarchicalDataTemplate>
</MenuItem.Resources>
</MenuItem>
The result is my MenuItem with a popup, but the subitems (quantities) don't have any bound text on them. The number of subitems is correct, but they have no children themselves. So i suppose there is a problem with the bindings, as the fixed Text i added to check if the DataTemplates actually work is showing ("Quantity").
I think i can't use ItemTemplate for the MenuItem as this is hierarchical with 2 different types...
EDIT:
My datamodel looks like this:
public class Quantity
{
[XmlAttribute]
public string Name;
[XmlElement]
public List<Measurement> Measurement;
}
public class Measurement
{
[XmlAttribute]
public string Name;
[XmlAttribute]
public string Symbol;
[XmlAttribute]
public string System;
public string ToBaseFormula;
public string FromBaseFormula;
}
回答1:
Oh man, glad you asked about the data model... now i figured it out:
The solution is: Use Properties in the data model, not Fields!
来源:https://stackoverflow.com/questions/15971101/creating-menuitems-from-list-with-bindings-results-in-blank-items