【发布时间】:2011-10-16 00:33:04
【问题描述】:
我有两个用户控件,第一个带有一个绑定到客户列表的列表框,该列表显示每个客户的一些简单详细信息。
第二个用户控件我想更详细地了解在第一个用户控件的列表框中选择的任何客户。
是否可以在第二个控件中设置绑定以绑定到第一个用户控件中的选定项?
我的列表框:
<ListBox Name="lstCustomer" ItemsSource="{Binding Customers}" >
<ListBox.Resources>
<DataTemplate DataType="{x:Type MyApplication:Customers}">
<Label Grid.Row="0" Content="{Binding Customer.name}" FontSize="14" FontWeight="Bold" Padding="5" />
<Label Grid.Row="1" Grid.Column="0" Content="{Binding Customer.telephone}" Padding="10,5" />
</Grid>
</Grid>
</DataTemplate>
</ListBox.Resources>
</ListBox>
详细视图用户控件(到目前为止)
<Grid x:Name="containingGrid" DataContext="{Binding ElementName=lstCustomers, Path=SelectedItem}">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Customer.name}" FontSize="23"/>
</Grid>
谢谢 格雷格
【问题讨论】:
-
如果您将 TextBlock 绑定更改为 Text="{Binding name}",您应该可以使用。我建议您在 Customer 的属性声明中将“名称”更改为“名称”,以将其标识为属性并使其遵循一些“规则!”。
标签: wpf data-binding user-controls listbox selecteditem