views:

59

answers:

1

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?

+1  A: 

The Linq2Sql ORM auto generates properties that represent related records based on relations in the model. In your case I would guess that if you change your binding of OwnerTextBlock to the following - you'll get what you want:

<TextBlock Name="OwnerTextBlock" Text="{Binding Employee.FirstName}" /> 

(I've only bound FirstName because to merge first and last names using XAML requires a MultiBinding and an IMultiValueConverter implementation - which is out of scope for this answer :) )

Remember that there must be an ORM relation between the related entities, for this to work.

Aviad P.
You have no idea how many times I have Googled on Bing and Binged on Google trying to get to the related records, I have used them in WinForms but could not find a way to bind to them. I had to change Employee.FirstName to Employee.EmpFirstName but this did the trick.I have done IValueConverters to I should have no problems with an IMultiValueConverter.THANKS
Mike B