locked
WPF Application with TreeView Control (Data Binding) RRS feed

  • Question

  • Hi

    I am developing an WPF application. The basic operational environment can be configured and saved in xml file. I would like to let user add/delete/edit the TreeView nodes. The TreeView's TreeViewItems are bound to xml's hierarchical structure (3 levels - publication, site, and section).

    

    I cannot get the XmlDocument of the configuration file for updating. 

    Here is how I create the data access class for data binding below.

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Windows;
    using System.Xml;
    
    
    namespace WPFConfig.ConfigDataAccess
    {
    
        public class Configurations : List<Publication>
        {
            public XmlDocument MydocEnv;
    
    
            public Configurations()
            {
                Publication pub;
                string strPath4Env = Environment.CurrentDirectory + "\\Environment.xml";
                try
                {
                    XmlDocument docEnv = new XmlDocument();
    
                    MydocEnv = docEnv;
    
                    // Try to load Environment.xml from the working directory
                    if (File.Exists(strPath4Env))
                    {
                        // File Environment.xml exists
    
                        docEnv.Load(strPath4Env);
                    }
                    else
                    {
                        // File Environment.xml does not exists. 
                        // Create new Environment.xml
                        docEnv.LoadXml("<?xml version='1.0' encoding='UTF-8'?><Config><Publications></Publications><Paths/><Mics/></Config>");
    
                        docEnv.Save(strPath4Env);
                    }
    
                    if (docEnv.DocumentElement.SelectNodes("Publications/Publication").Count == 0)
                    {
                        XmlElement pub2Add = docEnv.CreateElement("Publication");
    
                        pub2Add.SetAttribute("name", "ToChange");
                        docEnv.DocumentElement.SelectSingleNode("Publications").AppendChild(pub2Add);
                    }
    
                    foreach (var Pubitem in docEnv.DocumentElement.SelectNodes("Publications/Publication"))
                    {
                        XmlElement xmlPubItem = (XmlElement)Pubitem;
    
                        Add(pub = new Publication(xmlPubItem));
    
                        foreach (var Siteitem in xmlPubItem.SelectNodes("Site"))
                        {
                            XmlElement xmlSiteItem = (XmlElement)Siteitem;
                            Site site2Add = new Site(xmlSiteItem);
    
                            foreach (var SectionItem in xmlSiteItem.SelectNodes("Section"))
                            {
                                XmlElement xmlSectionItem = (XmlElement)SectionItem;
                                Section section2Add = new Section(xmlSectionItem);
                                site2Add.Sections.Add(section2Add);
                            }
    
                            pub.Sites.Add(site2Add);
                        }
                    }
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.Message);
                }
            }
        }
    }
    

    Here is my MainWindow.xaml.

    <Window x:Class="WPFConfig.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:WPFConfig.ConfigDataAccess"
            mc:Ignorable="d"
            Title="ImpPlanner Configuration Tool" Height="400" Width="600">
        <Border Padding="2">
            <Grid>
                <!--    Grid in the main
                    There are 2 columns -->
                <Grid.ColumnDefinitions >
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
    
                <!--Column 0-->
                <Grid Grid.Column="0">
                    <Grid.RowDefinitions>
                        <RowDefinition/>
                        <RowDefinition/>
                        <RowDefinition/>
                        <RowDefinition/>
                        <RowDefinition/>
                        <RowDefinition/>
                        <RowDefinition/>
                        <RowDefinition/>
                        <RowDefinition/>
                    </Grid.RowDefinitions>
                    <GroupBox x:Name="gbPublication" Grid.Row="0" Grid.RowSpan="2" Header="Publication" BorderBrush="Gray" BorderThickness="1">
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition/>
                                <RowDefinition/>
                            </Grid.RowDefinitions>
                            <StackPanel Grid.Row="0" Orientation="Horizontal">
                                <Label Margin="0,0,10,0" HorizontalAlignment="Right" VerticalAlignment="Center">Publication Name</Label>
                                <TextBox x:Name="tbPublicationName" Margin="0,2" Padding="2" Width="150" VerticalAlignment="Center" LostFocus="tbPublicationName_LostFocus"/>
                            </StackPanel>
                            <StackPanel Grid.Row="1" Orientation="Horizontal">
                                <Button x:Name="btnAddPub" Width="100" Margin="10,3">Add</Button>
                                <Button x:Name="btnDelPub" Width="100" Margin="10,3">Delete</Button>
                            </StackPanel>
                        </Grid>
                    </GroupBox>
                    <GroupBox x:Name="gbSite" Grid.Row="2" Grid.RowSpan ="3" Header="Site" BorderBrush="Gray" BorderThickness="1">
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition/>
                                <RowDefinition/>
                                <RowDefinition/>
                            </Grid.RowDefinitions>
                            <StackPanel Grid.Row="0" Orientation="Horizontal">
                                <Label Margin="0,0,10,0" HorizontalAlignment="Right" VerticalAlignment="Center">Site Name</Label>
                                <TextBox x:Name="tbSiteName" Margin="0,2" Padding="2" Width="175" VerticalAlignment="Center"></TextBox>
                            </StackPanel>
                            <StackPanel Grid.Row="1" Orientation="Horizontal">
                                <Label Margin="0,0,10,0" HorizontalAlignment="Right" VerticalAlignment="Center">Active in System</Label>
                                <RadioButton  x:Name="rbServer1" VerticalAlignment="Center" IsChecked="True" Margin="0,0,10,0">Server 1</RadioButton>
                                <RadioButton  x:Name="rbServer2" VerticalAlignment="Center">Server 2</RadioButton>
                            </StackPanel>
                            <StackPanel Grid.Row="2" Orientation="Horizontal">
                                <Button x:Name="btnAddSite" Width="100" Margin="10,5">Add</Button>
                                <Button x:Name="btnDelSite" Width="100" Margin="10,5">Delete</Button>
                            </StackPanel>
                        </Grid>
                    </GroupBox>
                    <GroupBox x:Name="gbSection" Grid.Row="5" Grid.RowSpan ="4" Header="Section" BorderBrush="Gray" BorderThickness="1">
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition/>
                                <RowDefinition/>
                                <RowDefinition/>
                                <RowDefinition/>
                            </Grid.RowDefinitions>
                            <StackPanel Grid.Row="0" Orientation="Horizontal">
                                <Label Margin="0,0,10,0" HorizontalAlignment="Right" VerticalAlignment="Center">Section Name</Label>
                                <TextBox x:Name="tbSectionName" Margin="0,2" Padding="2" Width="175" VerticalAlignment="Center"></TextBox>
                            </StackPanel>
                            <StackPanel Grid.Row="1" Orientation="Horizontal">
                                <Label Margin="0,0,10,0" HorizontalAlignment="Right" VerticalAlignment="Center">Page Type</Label>
                                <ComboBox x:Name="cbPageType" Width="150" Margin="5">
                                    <ComboBoxItem IsSelected="True">Broadsheet</ComboBoxItem>
                                    <ComboBoxItem>Tabloid</ComboBoxItem>
                                </ComboBox>
                            </StackPanel>
                            <StackPanel Grid.Row="2" Orientation="Horizontal">
                                <Label Margin="0,0,10,0" HorizontalAlignment="Right" VerticalAlignment="Center">Layout Group Name</Label>
                                <TextBox x:Name="tbLayoutGroupName" Margin="0,2" Padding="2" Width="150" VerticalAlignment="Center"></TextBox>
                            </StackPanel>
                            <StackPanel Grid.Row="3" Orientation="Horizontal">
                                <Button x:Name="btnAddSection" Margin="10,5" Padding="2" Width="100">Add</Button>
                                <Button x:Name="btnDelSection" Margin="10,5" Padding="2" Width="100">Delete</Button>
                            </StackPanel>
    
                        </Grid>
                    </GroupBox>
                </Grid>
                <Grid Grid.Column="1">
                    <DockPanel x:Name="dp4TreeView">
                        <DockPanel.Resources>
                            <local:Configurations x:Key="MyList"/>
    
                            <HierarchicalDataTemplate DataType="{x:Type local:Publication }"
                                    ItemsSource = "{Binding Path=Sites}">
                                <StackPanel Orientation="Horizontal">
                                    <Image Width="20" Height="20" Margin="3,0" Source="Images\Pub.ico"/>
                                    <TextBlock Text="{Binding Path=Name }"/>
                                </StackPanel>
                            </HierarchicalDataTemplate>
    
                            <HierarchicalDataTemplate DataType="{x:Type local:Site}"
                                    ItemsSource = "{Binding Path=Sections}">
                                <StackPanel Orientation="Horizontal">
                                    <Image Width="20" Height="20" Margin="3,0" Source="Images\Site.ico" />
                                    <TextBlock Text="{Binding Path=Name }"/>
                                </StackPanel>
                            </HierarchicalDataTemplate>
    
                            <DataTemplate DataType="{x:Type local:Section}">
                                <StackPanel Orientation="Horizontal">
                                    <Image Width="20" Height="20" Margin="3,0" Source="Images\Section.ico" />
                                    <TextBlock Text="{Binding Path=Name }"/>
                                </StackPanel>
                            </DataTemplate>
                        </DockPanel.Resources>
    
                        <TreeView>
                            <TreeViewItem x:Name="tvPubConfig" ItemsSource="{Binding Source={StaticResource MyList}}" 
                                      Header="Publications Configuration" TreeViewItem.Selected="TreeViewItem_Selected"/>
                        </TreeView>
    
                    </DockPanel>
                </Grid>
            </Grid>
        </Border>
    
    </Window>
    

    How should I do so that my MainWindow.xaml.cs can get the XmlDocument for further data input validation and for saving?

    • Moved by CoolDadTx Thursday, May 21, 2020 2:25 PM WPF related
    Thursday, May 21, 2020 8:47 AM

All replies

  • Please post questions related to WPF in the new Microsoft Q&A forums.

    Michael Taylor http://www.michaeltaylorp3.net

    Thursday, May 21, 2020 2:25 PM