使用绑定从列表创建菜单项会导致空白项 [英] Creating menuitems from list with bindings results in blank items

查看:52
本文介绍了使用绑定从列表创建菜单项会导致空白项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Menuitem,该菜单项应显示为子项的数量列表,该菜单项显示为此数量定义的子项的单位列表.数量列表是在MenuItem的ItemsSource的代码后面设置的.

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>

结果是带有弹出菜单的MenuItem,但是子项(数量)上没有任何绑定文本.子项目的数量是正确的,但是它们本身没有子级.因此,我认为绑定存在问题,因为我添加了用于检查DataTemplates是否实际工作的固定文本(数量").

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").

我认为我不能将ItemTemplate用于MenuItem,因为这是具有2种不同类型的分层结构...

I think i can't use ItemTemplate for the MenuItem as this is hierarchical with 2 different types...

我的数据模型如下:

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;
}

推荐答案

哦,很高兴您问到有关数据模型的信息……现在我知道了:

Oh man, glad you asked about the data model... now i figured it out:

解决方案是:使用数据模型中的属性",而不是字段"!

这篇关于使用绑定从列表创建菜单项会导致空白项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆