0

I have a MainWindow.xaml which looks like below:

<TabControl TabStripPlacement="Left">
    <TabItem Header="Display Tree Data Details" HorizontalAlignment="Left">
         <uControls:DisplayDataUserControl />
    </TabItem>

   <TabItem Header="Configuration" HorizontalAlignment="Left">
        ------
    </TabItem>
   <TabItem Header="About" HorizontalAlignment="Left">
        ------
    </TabItem>
   <TabItem Header="Sponsors" HorizontalAlignment="Left">
        ------
    </TabItem>
</TabControl>

My DisplayDataUserControl uses another UserControl called treeUserControl. I made the treeUserControl so that I can reuse it anywhere I want in my WPF app.

<UserControl x:Class="WpfApplication2.DisplayDataUserControl "
         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" 
         x:Name="thisUC"
         d:DesignHeight="300" d:DesignWidth="300">
<Grid>
     <local:treeUserControl />
     <Grid>
       <!-- display the details of the selected treeviewitem from "treeUserControl" here.  -->
     </Grid>
</Grid>
</UserControl>

And the treeUserControl is a UserControl which displays tree data by extending TreeView with a DependecyPropety (SelectedItem_), as defined below :

<UserControl x:Class="WpfApplication2.treeUserControl"
         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" 
         x:Name="thisUC"
         d:DesignHeight="300" d:DesignWidth="300">
<Grid>
    <local:ExtendedTreeView ItemsSource="{Binding Items}" 
                            SelectedItem_="{Binding MyTreeSelectedItem, Mode=TwoWay}">
         .....
    </local:ExtendedTreeView>
 </Grid>

DependencyProperty definition :

public class ExtendedTreeView : TreeView
{
    public ExtendedTreeView() : base()
    {
        this.SelectedItemChanged += new RoutedPropertyChangedEventHandler<object>(___ICH);
    }

    void ___ICH(object sender, RoutedPropertyChangedEventArgs<object> e)
    {
        if (SelectedItem != null)
        {
            SetValue(SelectedItem_Property, SelectedItem);
        }
    }

    public object SelectedItem_
    {
        get { return (object)GetValue(SelectedItem_Property); }
        set { SetValue(SelectedItem_Property, value); }
    }
    public static readonly DependencyProperty SelectedItem_Property = DependencyProperty.Register("SelectedItem_", typeof(object), typeof(ExtendedTreeView), new UIPropertyMetadata(null));
}

Can I access treeUserControl's MyTreeSelectedItem viewmodel property in DisplayDataUserControl so that it can display the detailed information about selected tree view item?

krrishna
  • 2,050
  • 4
  • 47
  • 101

1 Answers1

0

Your binding hierarchy looks wrong.

What you have now looks to be

<TabControl>
    <TabItem>
        <TreeUserControl>
            <ExtendedTreeView ItemsSource="{Binding Items}" SelectedItem="{Binding SelectedItem}" />
        </TreeUserControl>
    </TabItem>
    ...
</TabControl>

What you should have is something like this :

<TabControl ItemsSource="{Binding Items}" SelectedItem="{Binding SelectedItem}">
    <TabItem>                    <!-- DataContext is SelectedItem -->
        <TreeUserControl>        <!-- DataContext is SelectedItem -->
            <ExtendedTreeView /> <!-- DataContext is SelectedItem -->
        </TreeUserControl>
    </TabItem>
    ...
</TabControl>

In this second example, the .DataContext of your TabItem, TreeUserControl, and ExtendedTreeView will all be SelectedItem (which will be one of the items in the Items collection) because of the way the DataContext is by default inherited from the parent object.

I realize you're using Items in the ExtendedTreeView and I'm using it in the TabControl, but without knowing the correct structure of your application I can't offer you any suggestions there. This should hopefully give you an idea of what you're doing wrong though so you can correct it.

Community
  • 1
  • 1
Rachel
  • 130,264
  • 66
  • 304
  • 490
  • As described in my OP , i want to use this TreeUserControl in several places(as i am going to add 3 more tabs ) of my application. The one place where i am currently required to use is in the first tab ""Display Tree Data Details"" which will not only display the treeitem but also details of the selecteditem. I have another tab which i am going to add which also uses this treeUserControl in its DataGrid cell to make selection o item(just like a drop down). For this reason i made it user control and to get the selecteditem i added the Dep Property.There are more tabs coming up not just one. – krrishna Sep 22 '16 at 18:32
  • We Create Employee Hierarchy with who reports to who . In one tab our focus is only to create the hierarchy and in another tab we display details of the (employee) treenode. In another tab we want to make changes to the selected employees job titles with grid having two columns with one for selecting the employee(we use tree here again) and in another column we select the new title from drop down.Hope i gave you some background about the app which can help understand this scenario. – krrishna Sep 22 '16 at 18:41
  • 1
    @krrishna That's fine, if that's the case though you should be binding the `ExtendedTreeView` properties from the `TreeUserControl` level (and keep going up the tree like that). The point is the data behind the control (`.DataContext`) should **be passed to** the `TreeUserControl`, and **not be originating from** that control. I write answers about this all the time on here, such as [this one](http://stackoverflow.com/a/16488618/302677) – Rachel Sep 22 '16 at 18:41