Reputation: 6668
In my class ResultEntity
if I do:
_resultMulti = new List<ResultTaskFund>();
I see the following error message:
"Exception: Items collection must be empty before using ItemSource."
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?
I have the following classes:
ResultSummary
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; }
}
Result Fund
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>
Upvotes: 3
Views: 100
Reputation: 4591
Just wrap your column definitions with DataGrid.Columns. As you are binding it using ItemsSource you can't put any direct content in the definition. It is considering your columns as items of the grid which can't go with ItemsSource collection.
Upvotes: 0
Reputation: 5789
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>
Upvotes: 4