简体   繁体   中英

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 am trying to do is have a column of comboboxes inside a datagrid, and each combobox have several checkboxes. Each row of the datagrid represent hospitals. The combobox will show which specialties the hospital has, and the user should also be able to modify these selections.

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 trying to load with only .Net Framework 4.0. The project is targeted for .Net Framework 4.0.

I suppose the reason for that is that the MultiBinding is using the RelativeSource and the DataGridColumn is not a part of visual tree. They must have fixed the column binding behaviour in 4.5. I got the same issue woth my code that looked like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM