将分层对象数据绑定到 ListBox [英] Binding Hierarchical object data to ListBox

查看:26
本文介绍了将分层对象数据绑定到 ListBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 WPF 中的数据绑定使用来自对象源的数据填充 ListBox.

I'm trying to populate a ListBox with data from an object source using data binding in WPF.

源是一个 ObjectDataProvider,它的数据是从 xml 文件中加载的.我读入 XML 文件,填写适当的数据结构,然后将 ObjectDataProvider 的 ObjectInstance 设置为数据结构.

The source is an ObjectDataProvider whose data is loaded in from an xml file. I read in the XML file, filling in the appropriate data structure, then set the ObjectInstance for the ObjectDataProvider to the data structure.

这是数据结构:

public class Element
{
       public decimal myValue;

       public decimal df_myValue { get { return myValue; } set { this.myValue = value; } }
}

public class BasicSet
{
       public Element[] elementSet;

       public Element[] df_elementSet { get { return elementSet; } set { this.elementSet = value; } }
}

public class DataSet
{
        public BasicSet[] basicSet;

    public BasicSet[] df_basicSet { get { return basicSet; } set { this.basicSet = value; } }
}

这是 XAML:

<UserControl.Resources>
    <ResourceDictionary>
        <ObjectDataProvider x:Key="TheData" />

        <DataTemplate x:Key="ElementTemplate">
            <StackPanel>
                <TextBox Text="{Binding, Path=df_myValue}" />
            </StackPanel>
        </DataTemplate>

        <HierarchicalDataTemplate x:Key="ElementSetTemplate" ItemsSource="{Binding Path=df_elementSet}" ItemTemplate="{StaticResource ElementTemplate}">
        </HierarchicalDataTemplate>

    </ResourceDictionary>
</UserControl.Resources>

<Grid>
    <ListBox ItemsSource="{StaticResource TheData}" ItemTemplate="{StaticResource ElementSetTemplate}">
    </ListBox>
</Grid>

这是加载 xml 数据的代码隐藏:

Here is the code-behind where the xml data is being loaded:

    private DataSet m_dataSet;
    private ObjectDataProvider mODP;

    public void LoadXml(EditorContext context, XmlValidator.Context validator, XmlDocument doc)
    {
        mODP = FindResource("TheData") as ObjectDataProvider;

        XmlSerializer xs = new XmlSerializer(typeof(DataSet));
        XmlReader r = new XmlNodeReader(doc.DocumentElement);
        m_dataSet = (DataSet)xs.Deserialize(r);

        mODP.ObjectInstance = m_dataSet;
    }

期望的结果是 ListBox 将有一个 TextBox 用于数据结构中的每个元素.请注意,数据结构是分层的是有原因的.我无法展平数据结构以简化问题.

The desired result is that the ListBox would have a TextBox for each element in the data structure. Note that the data structure is hierarchical for a reason. I cannot flatten the data structure to simplify the problem.

我确定 xml 数据已正确加载到数据结构中,因为我可以放置一个断点并检查它,所有数据看起来都很好.但是当我运行程序时,ListBox 中什么也没有显示.

I am certain that the xml data is being loaded correctly into the data structure, because I can place a breakpoint and check it and all the data looks fine. But when I run the program, nothing shows up in the ListBox.

感谢任何帮助.

推荐答案

我发现我做错了.有几件事不对劲.以下是要点:

I figured out the things I was doing wrong. There were several things wrong. Here are the main points:

1) 我应该一直在使用 itemsControl

1) I should have been using an itemsControl

2) 我不需要使用 HierarchicalDataTemplate

2) I didn't need to use HierarchicalDataTemplate

3) 我需要三个级别的控件:在 itemsControl 中的一个 TextBox...这可以使用两个 DataTemplate 来完成.顶级 itemsControl 是指包含内部 itemsControl 的 DataTemplate.然后,itemsControl 引用包含内部 TextBox 的 DataTemplate.

3) I needed three levels of controls: a TextBox inside an itemsControl inside an itemsControl... and this can be accomplished using two DataTemplates. The top-level itemsControl refers to a DataTemplate that holds an inner itemsControl. That itemsControl then refers to a DataTemplate that holds an inner TextBox.

这是正确的 xaml:

And here is the correct xaml:

<UserControl.Resources>
    <ResourceDictionary>
        <ObjectDataProvider x:Key="TheData" />

        <DataTemplate x:Key="ElementTemplate">
            <TextBox Text="{Binding Path=df_myValue}"/>
        </DataTemplate>

        <DataTemplate x:Key="ElementSetTemplate">
            <GroupBox Header="I am a GroupBox">
                <ItemsControl ItemsSource="{Binding Path=df_elementSet}" ItemTemplate="{StaticResource ElementTemplate}">
                    <ItemsControl.ItemsPanel>
                        <ItemsPanelTemplate>
                            <StackPanel />
                        </ItemsPanelTemplate>
                    </ItemsControl.ItemsPanel>
                </ItemsControl>
            </GroupBox>
        </DataTemplate>

    </ResourceDictionary>
</UserControl.Resources>

<Grid>
    <ItemsControl ItemsSource="{Binding Source={StaticResource TheData}, Path=df_basicSet}" ItemTemplate="{StaticResource ElementSetTemplate}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
    </ItemsControl>
</Grid>

希望这对其他人有所帮助...

Hope this helps someone else...

这篇关于将分层对象数据绑定到 ListBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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