使用自定义列表使用ItemSource之前,Items集合必须为空 [英] Items collection must be empty before using ItemSource using custom list
问题描述
在我的班级 ResultEntity
中,如果我这样做:
In my class ResultEntity
if I do:
_resultMulti = new List<ResultTaskFund>();
我看到以下错误消息:
"Exception: Items collection must be empty before using ItemSource."
我看到很多人也收到此错误消息,并且我已经阅读了帖子,但是我不明白为什么我看到此消息.如果我只是声明 _resultMulti
而不初始化新列表,则应用程序将加载.我不明白为什么呢?
I have seen lots of people also have had this error message and I've read the posts but I don't understand why I'm seeing this message. If I just declare _resultMulti
without initialising a new list the application loads. I don't understand why though?
我有以下课程:
结果摘要
public class ResultEntity : INotifyPropertyChanged
{
public List<ResultTaskFund> ResultsMulti
{
get { return _resultsMulti; }
set { _resultsMulti = value; OnPropertyChanged("ResultsMulti"); }
}
List<ResultTaskFund> _resultMulti;
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
ResultTaskFund
public class ResultTaskFund : INotifyPropertyChanged
{
public string TaskName { get; set; }
public ResultFund ABBC { get; set; }
public ResultFund BBCA { get; set; }
public ResultFund CCCA { get; set; }
}
结果基金
public class ResultFund
{
public string FundCode { get; set; }
public ErrorAndWarningCodes ErrCode { get; set; }
public bool FundRequried { get; set; }
public bool CheckRequired { get; set; }
public string DisplayString { get; set; }
}
XAML
<DataGrid Grid.Row="0"
DataContext="{Binding ResultSummary}"
x:Name="dataGridResultMulti"
ItemsSource="{Binding ResultsMulti, UpdateSourceTrigger=PropertyChanged}"
Style="{StaticResource DataGridTemplate}"
ColumnHeaderStyle="{StaticResource DG_ColumnHeader}"
RowStyle="{StaticResource DG_Row}"
CellStyle="{StaticResource DG_Cell}"
RowDetailsTemplate="{StaticResource DG_RowDetail}"
RowHeaderStyle="{StaticResource DG_RowHeader}"
AutoGenerateColumns="False"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Background="Silver"
RowHeaderWidth="30"
Margin="25,5,20,15">
<DataGrid.Columns>
<DataGridTextColumn Header="Task" IsReadOnly="True" Binding="{Binding TaskName}"/>
<DataGridTextColumn Header="ABBC" IsReadOnly="True" Binding="{Binding ABBC.DisplayString}"/>
<DataGridTextColumn Header="BBCA" IsReadOnly="True" Binding="{Binding BBCA.DisplayString}"/>
<DataGridTextColumn Header="CCCA" IsReadOnly="True" Binding="{Binding CCCA.DisplayString}"/>
<DataGrid.Columns>
</DataGrid>
推荐答案
您直接将 DataGridTextColumns
作为 DataGrid
的项添加,因此您同时设置了 Items
和 ItemsSource
属性,您只能同时使用一个.通过使用 Columns
附加属性来修复您的列配置:
You are adding your DataGridTextColumns
directly as items of the DataGrid
, thus you are setting both the Items
and ItemsSource
property and you can only use one at the same time. Fix your columns configuration by using the Columns
attached property:
<DataGrid.Columns>
<DataGridTextColumn Header="Task" IsReadOnly="True" Binding="{Binding TaskName}"/>
<DataGridTextColumn Header="ABBC" IsReadOnly="True" Binding="{Binding ABBC.DisplayString}"/>
<DataGridTextColumn Header="BBCA" IsReadOnly="True" Binding="{Binding BBCA.DisplayString}"/>
<DataGridTextColumn Header="CCCA" IsReadOnly="True" Binding="{Binding CCCA.DisplayString}"/>
</DataGrid.Columns>
这篇关于使用自定义列表使用ItemSource之前,Items集合必须为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!