none
Does WPF TreeView work with ADO.NET DataTable? RRS feed

  • Question

  • Hi All!

    after doing some experimentation, and posting on WPF forum:

    http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/6536df47-c00c-4e66-b5f8-d8459a466127

    I came to the conclusion that WPF TreeView control works incorrecly with ADO.NET DataTable for some unknown reason.

    I have TreeView control bound to a DataTable. If I update rows in the DataTable then only first-level items of TreeView are updated. Here is the complete example:

    <Window x:Class="WpfApplication1.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:data="clr-namespace:System.Data;assembly=System.Data"
        Title="Window1" Height="331" Width="319" Loaded="Window_Loaded">
        <Window.Resources>
            <HierarchicalDataTemplate x:Key="ProjectTemplate" 
                                      DataType="{x:Type data:DataRowView}" 
                                      ItemsSource="{Binding Path=ProjectProject, 
                                      NotifyOnSourceUpdated=True, 
                                      NotifyOnTargetUpdated=True, 
                                      UpdateSourceTrigger=PropertyChanged}">
                <StackPanel>
                    <TextBlock Margin="0,3,0,0">
                                    <TextBlock.Text>
                                        <MultiBinding StringFormat="{}{0} (ProjectId={1})">
                                            <Binding Path="Name" />
                                            <Binding Path="ProjectId" />
                                        </MultiBinding>
                                    </TextBlock.Text>
                    </TextBlock>
                </StackPanel>
            </HierarchicalDataTemplate>
        </Window.Resources>
        <Grid>
            <ListBox Name="listbox" Height="100" VerticalAlignment="Top">
                <ListBox.Resources>
                    <DataTemplate DataType="{x:Type data:DataRowView}">
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="{Binding Name}" />
                            <TextBlock Text="   "/>
                            <TextBlock Text="{Binding ProjectId}"/>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.Resources>
            </ListBox>
            <Button Click="Button_Click" Content="update data source" Margin="3,0,0,2" Height="47" VerticalAlignment="Bottom" HorizontalAlignment="Left" Width="134" />
            <TreeView ItemTemplate="{Binding Source={StaticResource ProjectTemplate}}" Name="treeView1" Margin="3,106,0,55">
                <TreeView.Resources>
                </TreeView.Resources>
            </TreeView>
            <Button Height="47" Margin="143,0,0,2" Name="button1" VerticalAlignment="Bottom" HorizontalAlignment="Left" Width="103" Click="button1_Click">Update second row</Button>
        </Grid>
    </Window>
    

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    using System.Data;
    
    namespace WpfApplication1
    {
        /// <summary>
        /// Interaction logic for Window1.xaml
        /// </summary>
        public partial class Window1 : Window
        {
            public Window1()
            {
                InitializeComponent();
            }
    
            DataSet myds = new DataSet();
    
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                myds.Tables["ProjectTable"].Rows[0][0] = "new value";
                myds.Tables["ProjectTable"].Rows.Add(new object[] { "cc", 3, 2 });
            }
    
            private void Window_Loaded(object sender, RoutedEventArgs e)
            {
                DataTable m_dt = new DataTable("ProjectTable");
                m_dt.Columns.Add("Name", typeof(string));
                m_dt.Columns.Add("ProjectId", typeof(int));
                m_dt.Columns.Add("ParentProjectId", typeof(int));
                myds.Tables.Add(m_dt);
    
                DataColumn parentColumn =
                    m_dt.Columns["ProjectId"];
    
                DataColumn childColumn =
                    m_dt.Columns["ParentProjectId"];
    
                // Create DataRelation.
    
                DataRelation projectprojectRelation;
                projectprojectRelation = new DataRelation("ProjectProject",
                    parentColumn, childColumn, false);
                // Add the relation to the DataSet.
                myds.Relations.Add(projectprojectRelation);
    
                m_dt.Rows.Add(new object[] { "aa", 1, null });
                m_dt.Rows.Add(new object[] { "bb", 2, 1 });
    
                CollectionViewSource projectViewSource = new CollectionViewSource();
                projectViewSource.Source = myds.Tables[0].DefaultView;
    
                listbox.ItemsSource = projectViewSource.View;
                treeView1.ItemsSource = projectViewSource.View;
            }
    
            private void button1_Click(object sender, RoutedEventArgs e)
            {
                myds.Tables["ProjectTable"].Rows[1][0] = "new value";
            }
        }
    }
    
    could someone comment this?

    Thanks.
    • Moved by Yichun_Feng Wednesday, September 30, 2009 1:53 AM (From:ADO.NET DataSet)
    Friday, September 25, 2009 4:11 PM

All replies

  • Hi Dmitriano,

     

    This issue may due to the design of TreeView and DataTable.

    Since your project is based on WPF controls, people who knows more about WPF will have better understanding of TreeView control.

    I’ll move this thread to WPF forum and keep track of it.

     

     

    Best Regards

    Yichun Feng


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Wednesday, September 30, 2009 1:53 AM