在我的项目中,我需要将数据绑定到另一个用户控件中的用户控件。为了简洁起见,我创建了一个概念上类似但非常简单的项目。
想象一下,我正在创建一个电话簿应用程序,其中包含两个用户控件,看起来像下面这样。 窗口中的大蓝色框将是一个UserControl,它显示所有者的姓名(Jane Doe),其中每个黄色框也是一个UserControl,它们显示联系人的姓名和电话号码。
我有两个类来保存相关数据,如下所示:
在我的
当我运行这个程序时,我可以看到第一个UserControl(蓝色框)顶部的所有者名称。然而,它似乎没有正确地绑定其中的UCPerson用户控件,我得到了一个空列表,如下所示: 我的猜测是我没有正确地绑定到第一个UserControl中的ItemsControl。我对数据绑定还不太熟悉,似乎无法找出正确的方法。
这里我做错了什么?
想象一下,我正在创建一个电话簿应用程序,其中包含两个用户控件,看起来像下面这样。 窗口中的大蓝色框将是一个UserControl,它显示所有者的姓名(Jane Doe),其中每个黄色框也是一个UserControl,它们显示联系人的姓名和电话号码。
我有两个类来保存相关数据,如下所示:
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() { }
}
在我的
MainWindow
中,我使用ViewModel并将其绑定到PhoneBook
UserControl,如下所示:<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
是 ViewModel 中 PhoneBook
类的一个实例。
我的两个用户控件及其 DependancyProperties
如下所示。
UCPhoneBook 用户控件 (蓝色框):
在这里,我使用 ItemsControl
动态绑定 UCPerson
用户控件,以便在运行时添加任意数量的用户控件。
<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>
它的 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(黄色框):
<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>
它的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;
}
}
当我运行这个程序时,我可以看到第一个UserControl(蓝色框)顶部的所有者名称。然而,它似乎没有正确地绑定其中的UCPerson用户控件,我得到了一个空列表,如下所示: 我的猜测是我没有正确地绑定到第一个UserControl中的ItemsControl。我对数据绑定还不太熟悉,似乎无法找出正确的方法。
这里我做错了什么?