问题描述
大家好,我还有一个关于数据绑定的问题.
简要项目说明:
我在Window1.xaml中有一个列表框,该列表框显示SQL数据库中的数据.更具体地说,数据库包含三个字段,名称",地址"和电话号码",并且列表框仅显示名称"字段.
我在UserControl1.xaml中有三个标签,我想在Window1 Lisbox中显示所选项目的名称",地址"和电话号码".
问题:
是否可以使用数据绑定从另一个窗口中的字段获取信息?
UserControl1.xaml代码:(当标签在同一窗口中时此代码有效,但当标签在UserControl中时该代码无效)
Hi Guys, I have another question this time regarding Databinding.
Brief Project Description:
I have a ListBox in Window1.xaml that is displaying data from a SQL Database. More specifically, the Database contains three fields, "Name", "Address" and "Phone Number" and the ListBox is displaying only the "Name" field.
I have three Labels in UserControl1.xaml that i would like to display the "Name", "Address" and "Phone Number" of the selected item in the Window1 Lisbox.
Question:
Is it possible to use Databinding to get information from a field in another window?
UserControl1.xaml Code: (this code works when the label is in the same window but not when the Label is in the UserControl)
<Label x:Name="nameLabel"
Content="{Binding ElementName=listbox, Path=SelectedItem.Name}"/>
Window1.xaml代码:
Window1.xaml Code:
<Window.Resource>
<DataTemplate x:Key="listboxTemplate">
<TextBlock Text="{Binding Path=Name}"/>
</DataTemplate>
</Window.Resources>
<Grid>
<ListBox x:Name="listbox" ItemTemplate="{StaticResource listboxTemplate}"/>
</Grid>
var query = from s in dc.People
orderby s.Name
select s;
listbox.ItemsSource = query.ToList();
预先感谢您的帮助,
Alex
Thanks in advance for your help,
Alex
推荐答案
这篇关于WPF-用户控件中的数据绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!