C# WPF XAML binding to DataTable

Posted by LnDCobra on Stack Overflow See other posts from Stack Overflow or by LnDCobra
Published on 2010-04-08T10:06:09Z Indexed on 2010/04/12 7:33 UTC
Read the original article Hit count: 639

Filed under:
|
|
|
|

I have the following tables:

Company {CompanyID, CompanyName}
Deal {CompanyID, Value}

And I have a listbox:

<ListBox Name="Deals"
         Height="100" Width="420" Margin="0,20,0,0" HorizontalAlignment="Left" VerticalAlignment="Top"
         Visibility="Visible" IsSynchronizedWithCurrentItem="True"
         ItemsSource="{Binding}" SelectionChanged="Deals_SelectionChanged">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding companyRowByBuyFromCompanyFK.CompanyName}" FontWeight="Bold"  />
                <TextBlock Text=" -> TGS -> " />
                <TextBlock Text="{Binding BuyFrom}" FontWeight="Bold" />
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

As you can see, I want to display the CompanyName rather then the ID which is a foreign Key. The relation "companyRowByBuyFromCompanyFK" exists, as in Deals_SelectionChanged I can access companyRowByBuyFromCompanyFK property of the Deals row, and I also can access the CompanyName propert of that row.

Is the reason why this is not working because XAML binding uses the [] indexer? Rather than properties on the CompanyRows in my DataTable?

At the moment im getting values such as

  • -> TGS -> 3
  • -> TGS -> 4

What would be the best way to accomplish this?

  1. Make a converter to convert foreign keys using Table being referenced as custom parameter.

  2. Create Table View for each table? This would be long winded as I have quite a large number of tables that have foreign keys.

© Stack Overflow or respective owner

Related posts about c#

Related posts about wpf