将数据从 JSON 恢复到 WPF 时,DisplayMemberPath 为空
When restoring data from JSON into WPF the DisplayMemberPath is blank
我有一个 DataGridComboBox,并且 ItemsSource 和 SelectedItemBinding 都正确绑定。
XAML:
<Grid>
<Grid.Resources>
<CollectionViewSource Source="{Binding BicnList}" x:Key="bicnList"/>
</Grid.Resources>
<DataGrid ItemsSource="{Binding DgNamingConventionRows}"
<DataGrid.Columns>
<DataGridComboBoxColumn Header="Category"
ItemsSource="{Binding Source={StaticResource bicnList}}"
DisplayMemberPath="CategoryName"
SelectedItemBinding="{Binding BuiltInCategoryName, UpdateSourceTrigger=PropertyChanged}">
<DataGridComboBoxColumn.EditingElementStyle>
<Style TargetType="{x:Type ComboBox}"/>
</DataGridComboBoxColumn.EditingElementStyle>
</DataGridComboBoxColumn>
<!--............!-->
</DataGrid.Columns>
</DataGrid>
</Grid>
打开组合框时,所有选项都正确显示,当我 select 一个项目时,我也可以正确看到 DisplayMemberPath
。
我正在使用 json 保存此数据,这也是正确完成的。
但是,在关闭 window 之后,我想在再次打开 WPF 时恢复此数据。数据恢复,反序列化正常,但是Category Combobox下没有显示。 DisplayMemberPath 为空。这个问题的原因可能是什么?如有任何帮助,我们将不胜感激。
C#:
private ObservableCollection<BuiltInCategoryName> _bicnList = new
ObservableCollection<BuiltInCategoryName>();
[JsonIgnore]
public ObservableCollection<BuiltInCategoryName> BicnList
{
get
{
return _bicnList;
}
set
{
_bicnList = value;
NotifyPropertyChanged("BicnList");
}
}
public class BuiltInCategoryName : NotifierBase
{
//display Category.Name
//save BuiltInCategory.Id
public string CategoryName { get; set; }
public BuiltInCategory BuiltInCategoryId { get; set; }
public BuiltInCategoryName(Document doc, BuiltInCategory bic)
{
Category cat = null;
try
{
cat = Category.GetCategory(doc, bic);
}
catch
{ }
CategoryName = cat?.Name;
BuiltInCategoryId = bic;
}
public BuiltInCategoryName()
{
}
}
public class ParameterRow : NotifierBase
{
private BuiltInCategoryName builtInCategoryName ;
public BuiltInCategoryName BuiltInCategoryName
{
get { return builtInCategoryName ; }
set
{
SetNotify(ref builtInCategoryName , value);
}
}
private string patternText;
public string PatternText
{
get { return patternText; }
set
{
SetNotify(ref patternText, value);
}
}
private string comparisonValue;
public string ComparisonValue
{
get { return comparisonValue; }
set
{
SetNotify(ref comparisonValue, value);
}
}
public ParameterRow(BuiltInCategoryName category, string patternText, string comparison)
{
BuiltInCategoryName = category;
PatternText = string.IsNullOrEmpty(patternText) ? "" : patternText;
ComparisonValue = string.IsNullOrEmpty(patternText) ? "Prefix" : comparison;
}
public ParameterRow()
{
}
}
解决这个问题的方法不是使用 SelectedItemBinding
属性 我使用 SelectedValueBinding
.
XAML:
<Grid>
<Grid.Resources>
<CollectionViewSource Source="{Binding BicnList}" x:Key="bicnList"/>
</Grid.Resources>
<DataGrid ItemsSource="{Binding DgNamingConventionRows}"
<DataGrid.Columns>
<DataGridComboBoxColumn Header="Category"
ItemsSource="{Binding Source={StaticResource bicnList}}"
DisplayMemberPath="CategoryName"
SelectedValueBinding="{Binding BuiltInCategoryName, UpdateSourceTrigger=PropertyChanged}">
<DataGridComboBoxColumn.EditingElementStyle>
<Style TargetType="{x:Type ComboBox}"/>
</DataGridComboBoxColumn.EditingElementStyle>
</DataGridComboBoxColumn>
<!--............!-->
</DataGrid.Columns>
</DataGrid>
</Grid>
我有一个 DataGridComboBox,并且 ItemsSource 和 SelectedItemBinding 都正确绑定。
XAML:
<Grid>
<Grid.Resources>
<CollectionViewSource Source="{Binding BicnList}" x:Key="bicnList"/>
</Grid.Resources>
<DataGrid ItemsSource="{Binding DgNamingConventionRows}"
<DataGrid.Columns>
<DataGridComboBoxColumn Header="Category"
ItemsSource="{Binding Source={StaticResource bicnList}}"
DisplayMemberPath="CategoryName"
SelectedItemBinding="{Binding BuiltInCategoryName, UpdateSourceTrigger=PropertyChanged}">
<DataGridComboBoxColumn.EditingElementStyle>
<Style TargetType="{x:Type ComboBox}"/>
</DataGridComboBoxColumn.EditingElementStyle>
</DataGridComboBoxColumn>
<!--............!-->
</DataGrid.Columns>
</DataGrid>
</Grid>
打开组合框时,所有选项都正确显示,当我 select 一个项目时,我也可以正确看到 DisplayMemberPath
。
我正在使用 json 保存此数据,这也是正确完成的。
但是,在关闭 window 之后,我想在再次打开 WPF 时恢复此数据。数据恢复,反序列化正常,但是Category Combobox下没有显示。 DisplayMemberPath 为空。这个问题的原因可能是什么?如有任何帮助,我们将不胜感激。
C#:
private ObservableCollection<BuiltInCategoryName> _bicnList = new
ObservableCollection<BuiltInCategoryName>();
[JsonIgnore]
public ObservableCollection<BuiltInCategoryName> BicnList
{
get
{
return _bicnList;
}
set
{
_bicnList = value;
NotifyPropertyChanged("BicnList");
}
}
public class BuiltInCategoryName : NotifierBase
{
//display Category.Name
//save BuiltInCategory.Id
public string CategoryName { get; set; }
public BuiltInCategory BuiltInCategoryId { get; set; }
public BuiltInCategoryName(Document doc, BuiltInCategory bic)
{
Category cat = null;
try
{
cat = Category.GetCategory(doc, bic);
}
catch
{ }
CategoryName = cat?.Name;
BuiltInCategoryId = bic;
}
public BuiltInCategoryName()
{
}
}
public class ParameterRow : NotifierBase
{
private BuiltInCategoryName builtInCategoryName ;
public BuiltInCategoryName BuiltInCategoryName
{
get { return builtInCategoryName ; }
set
{
SetNotify(ref builtInCategoryName , value);
}
}
private string patternText;
public string PatternText
{
get { return patternText; }
set
{
SetNotify(ref patternText, value);
}
}
private string comparisonValue;
public string ComparisonValue
{
get { return comparisonValue; }
set
{
SetNotify(ref comparisonValue, value);
}
}
public ParameterRow(BuiltInCategoryName category, string patternText, string comparison)
{
BuiltInCategoryName = category;
PatternText = string.IsNullOrEmpty(patternText) ? "" : patternText;
ComparisonValue = string.IsNullOrEmpty(patternText) ? "Prefix" : comparison;
}
public ParameterRow()
{
}
}
解决这个问题的方法不是使用 SelectedItemBinding
属性 我使用 SelectedValueBinding
.
XAML:
<Grid>
<Grid.Resources>
<CollectionViewSource Source="{Binding BicnList}" x:Key="bicnList"/>
</Grid.Resources>
<DataGrid ItemsSource="{Binding DgNamingConventionRows}"
<DataGrid.Columns>
<DataGridComboBoxColumn Header="Category"
ItemsSource="{Binding Source={StaticResource bicnList}}"
DisplayMemberPath="CategoryName"
SelectedValueBinding="{Binding BuiltInCategoryName, UpdateSourceTrigger=PropertyChanged}">
<DataGridComboBoxColumn.EditingElementStyle>
<Style TargetType="{x:Type ComboBox}"/>
</DataGridComboBoxColumn.EditingElementStyle>
</DataGridComboBoxColumn>
<!--............!-->
</DataGrid.Columns>
</DataGrid>
</Grid>