Note that there are some explanatory texts on larger screens.

plurals
  1. POC# WPF XAML binding to DataTable
    text
    copied!<p>I have the following tables:</p> <p>Company {CompanyID, CompanyName}<br> Deal {CompanyID, Value}</p> <p>And I have a listbox:</p> <pre><code>&lt;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"&gt; &lt;ListBox.ItemTemplate&gt; &lt;DataTemplate&gt; &lt;StackPanel Orientation="Horizontal"&gt; &lt;TextBlock Text="{Binding companyRowByBuyFromCompanyFK.CompanyName}" FontWeight="Bold" /&gt; &lt;TextBlock Text=" -&gt; TGS -&gt; " /&gt; &lt;TextBlock Text="{Binding BuyFrom}" FontWeight="Bold" /&gt; &lt;/StackPanel&gt; &lt;/DataTemplate&gt; &lt;/ListBox.ItemTemplate&gt; &lt;/ListBox&gt; </code></pre> <p>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.</p> <p>Is the reason why this is not working because XAML binding uses the [] indexer? Rather than properties on the CompanyRows in my DataTable?</p> <p>At the moment im getting values such as</p> <ul> <li>-> TGS -> 3</li> <li>-> TGS -> 4</li> </ul> <p><strong>Edit</strong> Binding Error Update</p> <blockquote> <p>System.Windows.Data Error: 39 : BindingExpression path error: 'companyRowByBuyFromCompanyFK' property not found on 'object' ''DataRowView' (HashCode=30295189)'. BindingExpression:Path=companyRowByBuyFromCompanyFK.CompanyName; DataItem='DataRowView' (HashCode=30295189); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')</p> </blockquote> <p>Looks like its not converting the DataRowView item into a SuppliersRow</p> <p><strong>What would be the best way to accomplish this?</strong> </p> <ol> <li><p>Make a converter to convert foreign keys using Table being referenced as custom parameter. </p></li> <li><p>Create Table View for each table? This would be long winded as I have quite a large number of tables that have foreign keys.</p></li> </ol>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload