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?