I have been fighting this issue for some time so today I started a new project and simplified things to the basics. What I want to do is have a listbox, bound to a collection with a detail view of the selected item shown below. It all works fine except the items that refer to other tables simply show the foreign key. I was able to resolve this with a linq query but realize that the method I used is hackish.
public partial class Window1 : Window
{
DataClasses1DataContext db = new DataClasses1DataContext();
public IEnumerable<Issue> issues = null;
public Window1()
{
InitializeComponent();
issues = from i in db.Issues
select i;
this.DataContext = issues;
}
// The hacked in query that correctly displays the name instead of Key
private void IssueListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
List<Issue> _issues = issues.ToList();
int empl = _issues[IssueListBox.SelectedIndex].IssOwner;
OwnerTextBlock.Text = (from emp in db.Employees
where emp.EmpID == empl
select emp.EmpFirstName.ToString() + " " +
emp.EmpLastName.ToString()).Single();
}
}
The Xaml as simple as I could get it:
<Window.Resources>
<DataTemplate x:Key="ShowIssueDetail">
<TextBlock Text="{Binding Path=IssTitle}" FontWeight="Bold" FontSize="14"/>
</DataTemplate>
</Window.Resources>
<StackPanel>
<TextBlock Text="Issues" FontWeight="Bold" />
<ListBox x:Name="IssueListBox"
ItemsSource="{Binding}"
ItemTemplate="{StaticResource ShowIssueDetail}"
IsSynchronizedWithCurrentItem="True"
HorizontalContentAlignment="Stretch" SelectionChanged="IssueListBox_SelectionChanged">
</ListBox>
<TextBlock Text="{Binding Path=IssDescription}" />
<TextBlock Text="{Binding Path=IssOwner}" /> <!--I want this to show the name, not the Key-->
<TextBlock Name='OwnerTextBlock' /> <!--This has the name set in code from the Linq query and works-->
</StackPanel>
How do I do this correctly?