Click here to Skip to main content
15,662,532 members
Please Sign up or sign in to vote.
4.38/5 (4 votes)
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)
XML
<Label x:Name="nameLabel"
       Content="{Binding ElementName=listbox, Path=SelectedItem.Name}"/>

Window1.xaml Code:
XML
<Window.Resource>
    <DataTemplate x:Key="listboxTemplate">
        <TextBlock Text="{Binding Path=Name}"/>
    </DataTemplate>
</Window.Resources>
<Grid>
    <ListBox x:Name="listbox" ItemTemplate="{StaticResource listboxTemplate}"/>
</Grid>
C#
var query = from s in dc.People
            orderby s.Name
            select s;
listbox.ItemsSource = query.ToList();

Thanks in advance for your help,
Alex
Posted

1 solution

Answer: Remove the ElementName=listbox from the Label ContentBinding, then in the window, when you add the UserControl1 you simple add the following code:
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900