数据绑定不适用于使用Collectionview源的wpf中的Datagrid

时间:2014-08-05 09:58:55

标签: c# wpf collections

这里,数据网格的代码片段绑定为集合视图源的静态源。 但是数据网格没有显示所需的结果。

 <Window.Resources>
        <local:EDiscoveryCoreDataList x:Key="CoreData"/>
        <CollectionViewSource x:Key="cvsCoreData" Source="{StaticResource CoreData}" Filter="CollectionViewSource_Filter">
        </CollectionViewSource>
    </Window.Resources>
<Grid Grid.Row="2" Grid.Column="0">
            <Canvas x:Name="PanelCanvas1" >
                <DataGrid ItemsSource="{Binding Source={StaticResource cvsCoreData}}"  SelectedCellsChanged="Data_SelectedCellsChanged" SelectionUnit="Cell" IsReadOnly="True" Style="{StaticResource AzureDataGrid}" AutoGenerateColumns="False" x:Name="Data" Canvas.Top="29" Width="1063" Height="536" VerticalScrollBarVisibility="Auto">
                    <!--<DataGrid.Columns>
                        <DataGridTextColumn Header="Key Term" Binding="{Binding KeyTerm }" />
                        <DataGridTextColumn Header="Key Term Description" Binding="{Binding KeyTermDescription}" />
                        <DataGridTextColumn Header="Short Hand" Binding="{Binding ShortHand}" />
                        <DataGridTextColumn Header="Rule" Binding="{Binding Rule}" />
                    </DataGrid.Columns>-->
                    <DataGrid.RowDetailsTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Synonyms}"/>
                        </DataTemplate>
                    </DataGrid.RowDetailsTemplate>
                    <DataGrid.ContextMenu>
                        <ContextMenu>
                            <MenuItem Header="View in Document" />
                            <MenuItem Header="Add/Edit Key Terms or Synonyms" />
                            <MenuItem Header="Add/Edit Scoring Answers" />
                            <MenuItem Header="Edit Rule" />
                        </ContextMenu>
                    </DataGrid.ContextMenu>
                </DataGrid>

数据类:

public class EDiscoveryCoreData
    {
        internal string PartNo;
        internal string Section;
        private List<string> _synonyms;
        public string Synonyms
        {
            get
            {
                string returnstring = string.Empty;
                foreach (string synonyms in _synonyms)
                {
                    returnstring += synonyms + "\n";
                }
                return returnstring;
            }

        }
        public string KeyTerm { get; set; }

        public string KeyTermDescription { get; set; }

        public string ShortHand { get; set; }

        public string Rule { get; set; }

        public EDiscoveryCoreData() { }
        public EDiscoveryCoreData(string PartNo, string Section, string KeyTerm, string KeyTermDescription, string ShortHand, string Rule, List<string> synonyms):this() 
        {
            this.KeyTerm = KeyTerm;
            this.KeyTermDescription = KeyTermDescription;
            this.PartNo = PartNo;
            this.Rule = Rule;
            this.Section = Section;
            this.ShortHand = ShortHand;
            this._synonyms = synonyms;

        }


    }

集合类:

    public class EDiscoveryCoreDataList : ObservableCollection<EDiscoveryCoreData>
    {
        //public List<EDiscoveryCoreData> DataList { get; set; }

        public EDiscoveryCoreDataList():base()
        {
            //DataList = new List<EDiscoveryCoreData>();
        }
        public void Add( EDiscoveryCoreData data)
        {
            base.Add(data);
        }

        //public List<EDiscoveryCoreData> GetList() 
        //{
        //    return DataList;
        //}


        public List<string> GetPartNo()
        {
            List<string> partList = this.Select(data => data.PartNo).ToList();
            partList.Insert(0, "All");

            return partList.Distinct().ToList();
        }

        public List<string> GetSection()
        {
            List<string> sectionList = this.Select(data => data.Section).ToList();
            sectionList.Insert(0, "All");

            return sectionList.Distinct().ToList();
        }

        public List<EDiscoveryCoreData> GetFilterData(string partno, string section)
        {
            //Console.WriteLine(partno + "======" + section);
            if (section == null)
                section = string.Empty;
            List<EDiscoveryCoreData> filteredData;
            if (partno.Equals("All") && section.Equals("All"))
                return this.ToList();
            else if (partno.Equals("All") && !section.Equals(""))
                filteredData = this.Where(data => (data.Section.Equals(section))).ToList();
            else if (section.Equals("All"))
                filteredData = this.Where(data => (data.PartNo.Equals(partno))).ToList();
            else
                filteredData = this.Where(data => (data.PartNo.Equals(partno) && data.Section.Equals(section))).ToList();
            return filteredData;
        }

        public List<EDiscoveryCoreData> GetFilterData(string searchstring)
        {
            Console.WriteLine(searchstring);
            if (searchstring.Equals(string.Empty))
                return this.ToList();
            else
                return this.Where(data => (data.KeyTerm.ToUpper().Contains(searchstring.ToUpper()))).ToList();
        }

        public List<string> GetListofKeyTerm()
        {
            return this.Select(keyterm => keyterm.KeyTerm).ToList();

        }
    }
}

填充集合

internal EDiscoveryCoreDataList CoreDataList;
        public Brush BorderColor { get; set; }
        public MainWindow()
        {
            InitializeComponent();
            CoreDataList = (EDiscoveryCoreDataList)this.Resources["CoreData"];
            //CoreDataList.Add(new EDiscoveryCoreData("aa", "bb", "cc", "dd", "ee", "ff", new List<string> { "abc" }));
            PanelCanvas.Visibility = Visibility.Hidden;
            PanelCanvas1.Visibility = Visibility.Hidden;
            Data.SelectionMode = DataGridSelectionMode.Extended;
            BorderColor = new SolidColorBrush(Color.FromRgb(0xff, 0xff, 0xff));
        }


        internal void PopulateMainWindow(EDiscoveryCoreDataList coredatalist)
        {
            CoreDataList = coredatalist;
        } 

这里我通过从另一个子窗口调用该函数来填充该集合。

1 个答案:

答案 0 :(得分:1)

  

在dataGrid中看不到任何内容的第一个问题。

您已将AutoGenerateColumns设置为False,并在XAML中注释掉了“列”部分。 将AutoGenerateColumns设置为true 以获取列。

  

第二个问题,即dataGrid中没有可见的行。

PopulateMainWindow 方法中,您将内部列表 CoreDataList 设置为方法中传递的其他引用。但是这不会更新XAML中定义的资源CoreData (您的dataGrid实际绑定到的那个)

您需要在CoreData列表中为GUI添加对象以获取可以这样做的通知:

internal void PopulateMainWindow(EDiscoveryCoreDataList coredatalist)
{
    var actualList = (EDiscoveryCoreDataList)this.Resources["CoreData"];
    foreach(EDiscoveryCoreData coreData in coredatalist)
    {
       actualList.Add(coreData);
    }
}

注意 - 但是,理想的情景是在ViewModel中声明列表实例并将CollectionViewSource的Source绑定到该列表。因此,当您修改该列表时,GUI会收到通知,您不必从XAML资源中获取实例。