在使用自定义列表

时间:2015-12-17 08:24:07

标签: c# wpf xaml

我的班级ResultEntity如果我这样做:

_resultMulti = new List<ResultTaskFund>(); 

我看到以下错误消息:

"Exception: Items collection must be empty before using ItemSource."

我见过很多人也有这个错误信息,我已经阅读了帖子,但我不明白为什么我看到这个消息。如果我在没有初始化新列表的情况下声明_resultMulti,则应用程序会加载。我不明白为什么呢?

我有以下课程:

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; }         
    }

结果基金

    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>

2 个答案:

答案 0 :(得分:4)

您是将DataGridTextColumns直接添加为DataGrid的项目,因此您同时设置了ItemsItemsSource属性,并且您只能使用同一个Columns时间。使用<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> 附加属性修复列配置:

$(document).ready(function () {
    $("#ios").click(function(){
        $("#maincontent").load("ios.html",function(){
            $(".dropdown-toggle").dropdown();
        });
    });
});

答案 1 :(得分:0)

使用 DataGrid.Columns 包装您的列定义。当您使用ItemsSource绑定它时,您无法在定义中添加任何直接内容。它将您的列视为网格的项目,不能与ItemsSource集合一起使用。