Note that there are some explanatory texts on larger screens.

plurals
  1. POBinding Hierarchical object data to ListBox
    primarykey
    data
    text
    <p>I'm trying to populate a ListBox with data from an object source using data binding in WPF.</p> <p>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.</p> <p>Here is the data structure:</p> <p>public class Element { public decimal myValue;</p> <pre><code> public decimal df_myValue { get { return myValue; } set { this.myValue = value; } } </code></pre> <p>}</p> <p>public class BasicSet { public Element[] elementSet;</p> <pre><code> public Element[] df_elementSet { get { return elementSet; } set { this.elementSet = value; } } </code></pre> <p>}</p> <p>public class DataSet { public BasicSet[] basicSet;</p> <pre><code>public BasicSet[] df_basicSet { get { return basicSet; } set { this.basicSet = value; } } </code></pre> <p>}</p> <p>Here is the XAML:</p> <pre><code>&lt;UserControl.Resources&gt; &lt;ResourceDictionary&gt; &lt;ObjectDataProvider x:Key="TheData" /&gt; &lt;DataTemplate x:Key="ElementTemplate"&gt; &lt;StackPanel&gt; &lt;TextBox Text="{Binding, Path=df_myValue}" /&gt; &lt;/StackPanel&gt; &lt;/DataTemplate&gt; &lt;HierarchicalDataTemplate x:Key="ElementSetTemplate" ItemsSource="{Binding Path=df_elementSet}" ItemTemplate="{StaticResource ElementTemplate}"&gt; &lt;/HierarchicalDataTemplate&gt; &lt;/ResourceDictionary&gt; &lt;/UserControl.Resources&gt; &lt;Grid&gt; &lt;ListBox ItemsSource="{StaticResource TheData}" ItemTemplate="{StaticResource ElementSetTemplate}"&gt; &lt;/ListBox&gt; &lt;/Grid&gt; </code></pre> <p>Here is the code-behind where the xml data is being loaded:</p> <pre><code> 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; } </code></pre> <p>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.</p> <p>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.</p> <p>Any help is appreciated.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload