C # WPF Binding XAML to DataTable

I have the following tables:

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

And I have a list:

<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 company_name, not the identifier, which is the foreign key. The relationship "companyRowByBuyFromCompanyFK" exists, as in the Deals application. I can access the companyRowByBuyFromCompanyFK property in the "Transactions" line, and also access the CompanyName property of this line.

Is the reason why this does not work because the XAML binding uses the [] indexer? Instead of properties in CompanyRows in my DataTable?

At the moment im, getting values ​​like

  • β†’ TGS β†’ 3
  • β†’ TGS β†’ 4

Edit Update binding errors

System.Windows.Data error: 39: Binding pathion error: 'CompanyRowByBuyFromCompanyFK' property not found on 'object' 'DataRowView' (HashCode = 30295189) '. BindingExpression: Path = companyRowByBuyFromCompanyFK.CompanyName; DataItem = 'DataRowView' (HashCode = 30295189); target element "TextBlock" (Name = ''); the target property is β€œText” (type β€œString”)

It does not seem to convert the DataRowView element to a list of providers

What would be the best way to accomplish this?

  • Make a converter to convert foreign keys using the table referenced as a user parameter.

  • Create a table view for each table? This would be long because I have a fairly large number of tables with foreign keys.

+4
source share
2 answers

Someone will kill me ... thanks for pointing to the output window, which I always forgot about !: (

I found a solution by looking at the output window, which was as follows

System.Windows.Data error: 39: BindingExpression path error: 'CompanyRowByBuyFromCompanyFK' property not found on 'object' '' DataRowView

I realized that a DataTemplate binding is mandatory for a SelectedItem DataTemplate, which is a DataRowView, not the actual strong typed data. Fortunately, the DataRowView class has a Row property that points to the DataRow class (which is the provider), so I could communicate from there as usual. The fix was in the control line.

 <TextBlock Text="{Binding companyRowByBuyFromCompanyFK.CompanyName}" FontWeight="Bold" /> 

Changing this to the next worked as I expected.

 <TextBlock Text="{Binding Row.companyRowByBuyFromCompanyFK.CompanyName}" FontWeight="Bold" /> 
+2
source

How to set multi-connectivity in a datatemplate that converts a row (data source from a list) along with a hierarchical binding (which transfers a link to another table from the parent control) and the field name to another table to search for the identifier inside?

 <UserControl x:Class="local:MyUserControl"> <ListBox ItemsSource="{Binding Items}"> <ListBox.ItemTemplate> <DataTemplate> <Label> <Label.Content> <MultiBinding Converter="{StaticResource myConverter}"> <Binding Path="OtherId" /> <Binding RelativeSource="RelativeSource FindAncestor, AncestorType={x:Type local:MyUserControl}}}" Path="OtherTable" /> </MultiBinding> </Label.Content> </Label> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </UserControl> 

In the converter:

 public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture) { // TODO: Input type checks // TODO: Castings, find key in other table, return relevant field return (values[1] as IDictionary<String, String>)[values[0] as String]; } 
+1
source

Source: https://habr.com/ru/post/1306344/


All Articles