gpt4 book ai didi

c# - DataBinding 到 UserControl 内部的 UserControl

转载 作者:太空宇宙 更新时间:2023-11-03 20:58:25 31 4
gpt4 key购买 nike

<分区>

在我的项目中,我需要将数据绑定(bind)到驻留在另一个 UserControl 中的 UserControl。为了简洁起见,我创建了一个概念相似但非常简单的项目。

假设我正在创建一个电话簿应用程序,其中有两个用户控件,如下所示。

enter image description here

窗口中的大蓝色框将是一个 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 并像这样绑定(bind)到 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 动态绑定(bind) UCPerson UserControl,这样我就可以在运行时添加任意数量的控件。

<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 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;
}
}

当我运行它时,我可以在第一个用户控件(蓝色框)的顶部看到所有者的姓名,这很好。但是,它似乎没有正确绑定(bind) UCPerson 用户控件,我得到一个空列表,如下所示:

enter image description here

我的猜测是我没有正确绑定(bind)到第一个 UserControl 中的 ItemsControl。我是 DataBinding 的新手,似乎无法弄清楚正确的方法是什么。

我在这里做错了什么?

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com