In my project, I need to DataBind to a UserControl that resides in another UserControl. For the sake of brevity, I created a conceptually similar but very simple project.
Imagine that I am creating a Phone Book application, with two user controls in it, that looks like below.
The large blue box in the window would be one UserControl, which displays the owner's name (Jane Doe), and each of the yellow boxes within it are also UserControls, which display contacts' names and phone numbers.
I have two classes that I hold related data, as follows:
public class Person
{
public string Name { get; set; }
public string Phone { get; set; }
public Person() { }
}
public class PhoneBook
{
public string OwnerName { get; set; }
public ObservableCollection<Person> ContactList { get; set; }
public PhoneBook() { }
}
In my MainWindow
, I use a ViewModel and bind to the PhoneBook
UserControl like so:
<Window x:Class="UserControlDataBinding.MainWindow"
Title="MainWindow" Height="300" Width="350"
DataContext="{Binding Source={StaticResource mainViewModelLocator},Path=ViewModelPhoneBook}">
<Grid>
<local:UCPhoneBook x:Name="ucPhoneBook" MainPhoneBook="{Binding PhoneBookData}"></local:UCPhoneBook>
</Grid>
</Window>
PhoneBookData
is an instance of PhoneBook
class on the ViewModel.
My two user controls, and their DependancyProperties
look like below.
UCPhoneBook UserControl (the blue box):
Here I'm using an ItemsControl
to dynamically bind the UCPerson
UserControls so I can add as many as I like in runtime.
<UserControl x:Class="UserControlDataBinding.UCPhoneBook"
d:DesignHeight="300" d:DesignWidth="450">
<Canvas x:Name="ucCanvasPhoneBook" Background="White">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition/>
</Grid.RowDefinitions>
<GroupBox Grid.Row="0" Header="Phonebook">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
</Grid.RowDefinitions>
<Label Grid.Row="0" Name="lblOwnerName"
Content="{Binding Path=MainPhoneBook.OwnerName}">
</Label>
</Grid>
</GroupBox>
<ItemsControl Grid.Row="1"
ItemsSource="{Binding PhoneBookData.ContactList}">
<ItemsControl.ItemTemplate>
<DataTemplate DataType="{x:Type local:Person}">
<local:UCPerson PersonData="{Binding Person}"></local:UCPerson>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</Grid>
</Canvas>
</UserControl>
Its DependancyProperty
:
public partial class UCPhoneBook : UserControl
{
private static readonly DependencyProperty PhoneBookProperty = DependencyProperty.Register("MainPhoneBook", typeof(PhoneBook), typeof(UCPhoneBook), new PropertyMetadata(null));
public PhoneBook MainPhoneBook
{
get { return (PhoneBook)GetValue(PhoneBookProperty); }
set { SetValue(PhoneBookProperty, value); }
}
public UCPhoneBook()
{
InitializeComponent();
ucCanvasPhoneBook.DataContext = this;
}
}
UCPerson UserControl (the yellow boxes):
<UserControl x:Class="UserControlDataBinding.UCPerson"
d:DesignHeight="26" d:DesignWidth="400">
<Canvas x:Name="ucCanvasPerson" Background="WhiteSmoke">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Label Grid.Column="0" Name="lblName"
HorizontalAlignment="Left" VerticalAlignment="Center"
Content="{Binding Name}"></Label>
<Label Grid.Column="2" Name="lblPhone"
HorizontalAlignment="Right" VerticalAlignment="Center"
Content="{Binding Phone}"></Label>
</Grid>
</Canvas>
</UserControl>
Its DependancyProperty
:
public partial class UCPerson : UserControl
{
private static readonly DependencyProperty PersonProperty = DependencyProperty.Register("PersonData", typeof(Person), typeof(UCPerson), new PropertyMetadata(null));
public Person PersonData
{
get { return (Person)GetValue(PersonProperty); }
set { SetValue(PersonProperty, value); }
}
public UCPerson()
{
InitializeComponent();
ucCanvasPerson.DataContext = this;
}
}
When I run this, I can see the Owner's Name at the top of the first UserControl (blue box) just fine. However, it doesn't seem to correctly bind the UCPerson
user controls within, and I get an empty list like so:
My guess is that I'm not correctly binding to the ItemsControl
inside the first UserControl. I'm pretty new to DataBinding and can't seem to figure out what the correct approach is.
What am I doing wrong here?