Treeview Dynamically in WPF C#
Step 1) Create WPF Project using C# 4.0/4.5
Step 2) Add a Page ,name it as TreeViewdynamic.xaml
Step 3) Add TreeView control as shown below
<TreeView x:Name="TreeView1" HorizontalAlignment="Left" Height="233" VerticalAlignment="Top" Width="245" FontSize="16" Foreground="Beige">
<TreeViewItem Header="root">
</TreeViewItem>
</TreeView>
Add single treeview item name it as "root"
Step 4) Add Button control which will allow us to do popup screen for adding items
<Button Content="Add Node" Width="75" Height="30" x:Name="btnAdd" Click="btnAdd_Click_1" Margin="10,238,215,32"/>
Step 5) Add StackPanel with 2 controls in it
<TextBox Height="30" TextWrapping="Wrap" x:Name="txtNode" Text="" Width="204"/>
<Button Content="Add Node" Width="75" x:Name="btnAddNode" Click="btnAddNode_Click_1"/>
</StackPanel>
Step 6) Add Button Event handler for "Add Node"
Step 7) Button Inside StatckPanel, Which will Add items to TreeView.
stackpanel1.Opacity = 0;
//MakeTreeview background color to white
TreeView1.Background = new SolidColorBrush(Color.FromRgb(255, 255, 255));
//Select Root node. then add item otherwise MessageBox will popup.
TreeViewItem root = TreeView1.SelectedItem as TreeViewItem;
if (root != null)
{
TreeViewItem subItem = new TreeViewItem();
subItem.Header = txtNode.Text;
root.Items.Add(subItem);
root.IsExpanded = true;
//Expand the Node.
}
else MessageBox.Show("Select root/subitem");
Step 7) Run the Application
Click on "Add Node"
Tags:Treeview Dynamically Add items in WPF C#,Treeview Dynamically in WPF C#,dynamically add items in treeview wpf,TreeView dynamically add items in wpf using C#,TreeView Programatically add items in wpf using C#,TreeView-in-wpf
<Page x:Class="Wpfone.TreeViewDemo_dynamic"
using System;
Step 1) Create WPF Project using C# 4.0/4.5
Step 2) Add a Page ,name it as TreeViewdynamic.xaml
Step 3) Add TreeView control as shown below
<TreeView x:Name="TreeView1" HorizontalAlignment="Left" Height="233" VerticalAlignment="Top" Width="245" FontSize="16" Foreground="Beige">
<TreeViewItem Header="root">
</TreeViewItem>
</TreeView>
Add single treeview item name it as "root"
Step 4) Add Button control which will allow us to do popup screen for adding items
<Button Content="Add Node" Width="75" Height="30" x:Name="btnAdd" Click="btnAdd_Click_1" Margin="10,238,215,32"/>
Step 5) Add StackPanel with 2 controls in it
- TextBlock
- Button control
<TextBox Height="30" TextWrapping="Wrap" x:Name="txtNode" Text="" Width="204"/>
<Button Content="Add Node" Width="75" x:Name="btnAddNode" Click="btnAddNode_Click_1"/>
</StackPanel>
Step 6) Add Button Event handler for "Add Node"
- Which will set StatckPanel Opacity to 1(fully visible)
- stackpanel1.Opacity = 1;
- Now focus should goto StatckPanel rather than treeview control, so set background color for
TreeView to Gray - TreeView1.Background = new SolidColorBrush(Color.FromRgb(100, 100, 100));
Step 7) Button Inside StatckPanel, Which will Add items to TreeView.
stackpanel1.Opacity = 0;
//MakeTreeview background color to white
TreeView1.Background = new SolidColorBrush(Color.FromRgb(255, 255, 255));
//Select Root node. then add item otherwise MessageBox will popup.
TreeViewItem root = TreeView1.SelectedItem as TreeViewItem;
if (root != null)
{
TreeViewItem subItem = new TreeViewItem();
subItem.Header = txtNode.Text;
root.Items.Add(subItem);
root.IsExpanded = true;
//Expand the Node.
}
else MessageBox.Show("Select root/subitem");
Step 7) Run the Application
Click on "Add Node"
Tags:Treeview Dynamically Add items in WPF C#,Treeview Dynamically in WPF C#,dynamically add items in treeview wpf,TreeView dynamically add items in wpf using C#,TreeView Programatically add items in wpf using C#,TreeView-in-wpf
No comments:
Post a Comment