我有以下 DataGridTemplate 列:
<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>
我想要做的是在数据网格中有一列组合框,每个组合框都有几个复选框。数据网格的每一行代表医院。组合框将显示医院有哪些专业,用户还应该能够修改这些选择。
这是转换器的代码:
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;
}
}
这适用于使用 .Net Framework 4.5 的计算机,但在尝试仅使用 .Net Framework 4.0 加载时会崩溃。该项目针对.Net Framework 4.0。