WPF Multibinding.Net Framework 4.0

I have the following DataGridTemplate column:

<DataGridTemplateColumn x:Name="specialtiesColumn" Header="Specialties" MinWidth="170"> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <ComboBox ItemsSource="{Binding Path=DataContext.Specialties, Mode=OneWay, RelativeSource={RelativeSource FindAncestor, AncestorType=DataGrid}}"> <ComboBox.ItemTemplate> <DataTemplate> <StackPanel Height="17" VerticalAlignment="Center" Orientation="Horizontal"> <CheckBox Width="20"> <CheckBox.IsChecked> <MultiBinding Converter="{StaticResource ProviderSpecialtyIsInSpecialtiesConverter}"> <Binding RelativeSource="{RelativeSource FindAncestor, AncestorType=ComboBox}" Path="DataContext.Specialties" /> <Binding Path="Name" /> </MultiBinding> </CheckBox.IsChecked> </CheckBox> <TextBlock Text="{Binding Name}" Width="130" /> </StackPanel> </DataTemplate> </ComboBox.ItemTemplate> </ComboBox> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> 

What I'm trying to do is have a comboboxes column inside a datagrid, and there are several checkboxes in each combo box. Each row of data represents a hospital. The list box indicates which specialties the hospital has, and the user should also be able to change these parameters.

This is the code for the converter:

 public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) { try { HashSet<Specialty> specialties = (HashSet<Specialty>)values[0]; string specialty = (string)values[1]; foreach (Specialty s in specialties) { if (s.Name == specialty) return true; } return false; } catch (Exception) { return false; } } 

This works on computers with .Net Framework 4.5, but it crashes when you try to download only with .Net Framework 4.0. The project is intended for the .NET Framework 4.0.

+4
source share
1 answer

I believe the reason is that MultiBinding uses a RelativeSource and the DataGridColumn is not part of the visual tree. They should have fixed column binding behavior in 4.5. I have the same problem as my code that looks like this:

 <DataGridTextColumn.Binding> <MultiBinding Converter="{StaticResource directionConverter}"> <MultiBinding.Bindings> <Binding ElementName="clientPerspective" Path="IsChecked"/> <Binding Path="Direction"/> </MultiBinding.Bindings> </MultiBinding> </DataGridTextColumn.Binding>` 
0
source

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


All Articles