我有一个绑定到CollectionView(AvailableSampleTypes)的组合框。 在某些情况下,此列表可以没有项目(“SampleTypeOptionVisibility”在这种情况下崩溃),但组合框仍然试图绑定自身并引发异常。 我怎么处理这个?
<DockPanel Visibility="{Binding SampleTypeOptionVisibility}">
<TextBox Text="{x:Static properties:Resources.QC_SampleType}"/>
<ComboBox ItemsSource="{Binding Path=AvailableSampleTypes}"
DisplayMemberPath="DisplayName"
SelectedValuePath="Id"
SelectedValue="{Binding Path=SelectedSampleType}"
IsEnabled="{Binding IsSampleTypeEnabled}" />
</DockPanel>
public CollectionView AvailableSampleTypes
{
get { return new CollectionView(sampleTypeDefinitionsList); }
}
public string SelectedSampleType
{
get
{
var solution = SelectedSolution as IExpandedQcTestSolution;
if (solution != null)
{
return solution.SampleTypeId.ToString();
}
else
{
return sampleTypeDefinitionsList.ToList()[0].Id.ToString();
}
}
set
{
var solution = SelectedSolution as IExpandedQcTestSolution;
if (solution != null)
{
var sampleTypeDef = sampleTypeDefinitionsList.First(x => x.Id == new Guid(value));
solution.SampleTypeId = sampleTypeDef.Id;
}
}
}
“sampleTypeDefinitionsList”有一个“ExpandedQCSampleTypeDefinition”列表
public class ExpandedQCSampleTypeDefinition
{
public Guid Id { get; }
public string DisplayName { get; set; }
}