将分层对象数据绑定到 ListBox

发布于 2024-11-07 09:49:11 字数 2164 浏览 0 评论 0原文

我正在尝试使用 WPF 中的数据绑定使用来自对象源的数据填充列表框。

源是一个 ObjectDataProvider,其数据是从 xml 文件加载的。我读取 XML 文件,填充适当的数据结构,然后将 ObjectDataProvider 的 ObjectInstance 设置为该数据结构。

这是数据结构:

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 数据的代码隐藏:

    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。请注意,数据结构是分层的是有原因的。我无法通过扁平化数据结构来简化问题。

我确信 xml 数据已正确加载到数据结构中,因为我可以放置一个断点并检查它,并且所有数据看起来都很好。但是当我运行该程序时,列表框中没有显示任何内容。

任何帮助表示赞赏。

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

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.

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

Here is the 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>

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

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.

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.

Any help is appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

○愚か者の日 2024-11-14 09:49:11

我明白了我做错的事情。有几件事是错误的。以下是要点:

1)我应该使用 itemsControl

2)我不需要使用 HierarchicalDataTemplate

3)我需要三个级别的控件:itemsControl 内的 itemsControl 内的 TextBox ...这可以完成使用两个数据模板。顶级 itemsControl 引用包含内部 itemsControl 的 DataTemplate。然后,该 itemsControl 引用包含内部 TextBox 的 DataTemplate。

这是正确的 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>

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

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

1) I should have been using an itemsControl

2) I didn't need to use HierarchicalDataTemplate

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.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文