我正在尝试使用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; }所需的结果是,列表框将为数据结构中的每个元素都具有一个文本框.注意,出于某种原因,数据结构是分层的.我无法展平数据结构以简化问题.
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数据已正确加载到数据结构中,因为我可以放置一个断点并检查它,并且所有数据看起来都很好.但是,当我运行该程序时,列表框中没有任何显示.
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内的itemsControl内的TextBox ...这可以使用两个DataTemplates来完成.顶级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:
<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
发布评论