Creating menuitems from list with bindings results in blank items

南楼画角 提交于 2021-01-29 20:58:54

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!