Question

I'm querying a database via a stored procedure and getting data back. which I'm populating into an object as such:

private GetMiscVars _batchVariablesData;
        public GetMiscVars BatchVariablesData
        {
            get { return _batchVariablesData; }
            set
            {
                if (_batchVariablesData == value) return;
                _batchVariablesData = value;
                OnPropertyChanged("BatchVariablesData");
            }
        }  

In my XAML I have this:

<TreeView>
    <TreeViewItem Header="Batch Data" ItemsSource="{Binding BatchVariablesData}">
            <TreeViewItem.HeaderTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding VarDesc}" />
                </DataTemplate>
        </TreeViewItem.HeaderTemplate>
    </TreeViewItem>
</TreeView>  

What I am seeing when my code runs is my parent node is blank instead of saying "Batch Data" like I hoped and my child nodes return the name of the ObservableCollection used in calling the database, in this DgTabData.
So, where am I screwing things up?
Thanks

To clarify a little bit more, I have a stored procedure that returns data that looks like this:

VarId   EventId VarDesc
11      2489    WP1.WC01 Quantity
1196    2489    WP1.WC01 Brew Number
1197    2489    WP1.WC01 Operator Name
1216    2489    WP1.WC01 Integer Test
1217    2489    WP1.WC01 Logical Test
1218    2489    WP1.WC01 Float-4 Test
1219    2489    WP1.WC01 Float-3 Test  

I want to take the VarDesc field and place it in a TreeView under a heading of Batch Data. Once I can figure this out I want to be able to grab the VarId and EventId values from the item selected in the TreeView as I will use those to populate a datagrid.

Was it helpful?

Solution

Firstly, a TreeView displays hierarchical data. That means that your data must have a public collection property to use for the child nodes. Next, to display hierarchical data, you need to declare a HierarchicalDataTemplate, rather than trying to define the controls as you were. Try this XAML instead:

<TreeView ItemsSource="{Binding CollectionOfParents}">
    <TreeView.Resources>
        <HierarchicalDataTemplate DataType="{x:Type YourPrefix.YourClassName}" 
            ItemsSource="{Binding CollectionOfChildrenPropertyInParent}">
            <TextBlock Text="{Binding PropertyInParent}" /><!-- Parent DataTemplate -->
            <HierarchicalDataTemplate.ItemTemplate>
                <DataTemplate> <!-- Child DataTemplate -->
                    <TextBlock Text="{Binding PropertyInChild}" />
                </DataTemplate>
            </HierarchicalDataTemplate.ItemTemplate>
        </HierarchicalDataTemplate>
    </TreeView.Resources>
</TreeView>

UPDATE >>>

As I said, a TreeView displays hierarchical data. That means that your data must have a public collection property to use for the child nodes. So create a class, let's say named Data, with a property to hold the Batch Data text, let's say named Name, and an ObservableCollection<string> property, let's say named Items to hold the VarDesc column values.

Then you need to populate the collection with the items returned from the stored procedure in any way you see fit. Finally, create an ObservableCollection<Data> property, let's say named DataItems in the code behind or view model and add your Data class, complete with collection items. Then you could bind to the TreeView like this:

<TreeView ItemsSource="{Binding DataItems}">
    <TreeView.Resources>
        <HierarchicalDataTemplate DataType="{x:Type YourXamlNamespacePrefix.Data}" 
            ItemsSource="{Binding Items}">
            <TextBlock Text="{Binding Name}" /> <!-- Parent DataTemplate -->
            <HierarchicalDataTemplate.ItemTemplate>
                <DataTemplate> <!-- Child DataTemplate -->
                    <TextBlock Text="{Binding}" />
                </DataTemplate>
            </HierarchicalDataTemplate.ItemTemplate>
        </HierarchicalDataTemplate>
    </TreeView.Resources>
</TreeView>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top