none
WPF Databinding usercontrols

    Question

  • some one please help me.
    i have a tree control

        <UserControl x:Class="MyApp.Views.MyTree"
                     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                     mc:Ignorable="d" 
                     d:DesignHeight="300" d:DesignWidth="300">
            <Grid >
                <TreeView Name="myTreeControl" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="#FFB1D1DA" />
            </Grid>
        </UserControl>
    And a grid control

        <UserControl x:Class="MyApp.Views.MyOrder"
                     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                     mc:Ignorable="d" 
                    d:DesignHeight="300" d:DesignWidth="300">
            <Grid>
                <DataGrid Name="myGridControl" HorizontalAlignment="Stretch"  VerticalAlignment="Stretch" Background="#FFF7E5E5" />
            </Grid>
        </UserControl>

    I want to put this to some other control 

        <UserControl x:Class="MyApp.Views.ContainerPanel"
                     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                     xmlns:Views="clr-namespace:MyApp.Views"
                     mc:Ignorable="d" 
                     d:DesignHeight="300" d:DesignWidth="300">
            
            
            <Grid Name="grdContainer" >
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" ></RowDefinition>
                    <RowDefinition Height="*"></RowDefinition>
                    <RowDefinition Height="Auto"></RowDefinition>
                    <RowDefinition Height="*" ></RowDefinition>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"></ColumnDefinition>
                    <ColumnDefinition Width="Auto"></ColumnDefinition>
                    <ColumnDefinition Width="Auto"></ColumnDefinition>
                    <ColumnDefinition Width="Auto"></ColumnDefinition>
                </Grid.ColumnDefinitions>
        
                <Button Name="btnS" Content="S"  Width="25" Grid.Row="0" Grid.Column="1" HorizontalAlignment="Right" Height="25" Click="btnS_Click" />
                <Button Name="btnF" Content="F" Width="25"  Grid.Row="0" Grid.Column="2" Height="25" Click="btnF_Click"/>
                <Button Name="btnP" Content="P" Width="25" Height="25" Grid.Row="0" Grid.Column="3" Click="btnP_Click" />
        
                <Views:MyTree  x:Name="FT" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Row="1" Grid.ColumnSpan="4"  />
                
                <GridSplitter Name="GS" HorizontalAlignment="Stretch" Grid.Row="2" VerticalAlignment="Stretch" Height="9" ResizeBehavior="PreviousAndNext" Background="#FFC1B6B6" Grid.ColumnSpan="4" />
        
                <Views:MyOrder  x:Name="PO" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Row="3" Grid.ColumnSpan="4"  />   
        
        
        
            </Grid>
        </UserControl>

    On the button clicks it hide or show or split the screens.
    And now I have a main panel. And that hae a tab control. Tab control's itesm shuld be added dynamically. And each tab item is ConatainerPanel. And standing at MainPanel I have to add or remove items to MyTree and MyGrid..
    I would like to  use Property PropertyChangedEventHandler for doing these..
    Can any one help  me in this?
    Can any one give some sample codes?
    • Edited by Anu1987 Monday, August 26, 2013 12:18 PM
    Monday, August 26, 2013 12:02 PM

Answers

  • You should be able to use an ObservableCollection of ContainerPanel items.  It might seem a little unorthodox, but there is really nothing preventing you from doing that from what I can tell. 

    Christine A. Piffat

    • Marked as answer by Anu1987 Thursday, August 29, 2013 11:19 AM
    Wednesday, August 28, 2013 7:18 PM

All replies

  • Josh Smith has a very nice article on creating a TreeView with a ViewModel.  I found this very helpful when creating my first TreeView.  If you add a child to one of the nodes, you can add it to an ObservableCollection of the children for that node, and it should be reflected in the TreeView right away.  The same is true for removing a child.  If you want to make sure the users see the changes, you can set the IsExpanded property of that particular node to true once the child has been added or removed. 

    Hope that helps!


    Christine A. Piffat

    Monday, August 26, 2013 8:21 PM
  • Thanks for the reply . Let me go through the article.
    Tuesday, August 27, 2013 4:20 AM
  • I think that link is not explaining what I exactly want. I need a tab control and tab items are dynamically added. Tab item's item source is to be set.
    Tuesday, August 27, 2013 6:01 AM
  • You should be able to use an ObservableCollection of ContainerPanel items.  It might seem a little unorthodox, but there is really nothing preventing you from doing that from what I can tell. 

    Christine A. Piffat

    • Marked as answer by Anu1987 Thursday, August 29, 2013 11:19 AM
    Wednesday, August 28, 2013 7:18 PM
  • That worked for me.
    Thursday, August 29, 2013 11:19 AM