Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Add tree view item dynamically in WPF

$
0
0

I have two user controls like name usercontrol1 and usercontrol2  in usercontrol1 i have one tree view.

In another usercontrol2 i have textbox and save button. on click of save button i need to add this value of textbox to the tree view item of usercontrol1.

usercontrol2.xaml

<WrapPanel Grid.Column="0" Name="wrapPanelSecurtiyGroup" Width="700" Height="auto" VerticalAlignment="Top"HorizontalAlignment="Right">
                <Label  Name="lblName" Content="Name" HorizontalAlignment="Left"  VerticalAlignment="Top" Width="481"Margin="5,0,0,0" />
                <TextBox x:Name="txtName" VerticalContentAlignment="Center" Background="White" TextWrapping="NoWrap"Text="{Binding Name }" Margin="5,0,0,0" Width="611" Height="23"  >
                </TextBox>

<Button Name="btnSave" Content="Save" Click="Save_Click" Height="25" Width="100" Margin="120,0,0,0" />

</WrapPanel>

usercontrol2.xaml

<TreeView x:Name="controlTreeviewG" Background="{x:Null}" Margin="0,0,0,0" BorderBrush="{x:Null}">
                                    <TreeViewItem Header="GN" Tag="GN" FontSize="13"Name="treeGViewItem" ItemsSource="{Binding NItem}" >
                                    </TreeViewItem>
 </TreeView>

Code of usercontrol2.cs

                             

public partial class usercontrol2 : UserControl
    {
        private usercontrol1 _usercontrol1;

        public usercontrol2()
        {
            InitializeComponent();

        }

        private void Save_Click(object sender, RoutedEventArgs e)
        {
              //CLearing  children of wrapPanelAP

                //Setting Default Options
                _usercontrol1.treeGViewItem.Items.Add(txtName.Text.ToString());
                _usercontrol1.controlTreeviewG.Items.Refresh();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            _usercontrol1= new usercontrol1 ();
        }

    }

this is what i  am doing but its not working, please suggest something that can be helpful to me



Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>