简体   繁体   English

使用CollectionView源的数据绑定不适用于WPF中的Datagrid

[英]Data Binding not working for Datagrid in wpf using Collectionview source

Here the code snippet for the data grid bind as static source of collection view source. 此处,数据网格的代码段绑定为集合视图源的静态源。 But the datagrid not showing the required result. 但是数据网格未显示所需结果。

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

Data Class: 资料类别:

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;

        }


    }

Collection Class: 收集类:

    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();

        }
    }
}

Populate the collection 填充集合

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

Here I populate the collection by calling the the function from another child window. 在这里,我通过从另一个子窗口调用函数来填充集合。

First issue where nothing is visible in dataGrid. 第一个问题是dataGrid中什么都不可见。

You have set AutoGenerateColumns to False and also commented out columns section in XAML. 您已将AutoGenerateColumns设置为False,并且还注释了XAML中的column部分。 Set AutoGenerateColumns to true to get the columns. 将AutoGenerateColumns设置为true以获取列。

Second issue where no row is visible in dataGrid. 第二个问题是dataGrid中没有行可见。

In PopulateMainWindow method you set internal list CoreDataList to some other reference passed in the method. PopulateMainWindow方法中,将内部列表CoreDataList设置为该方法中传递的其他引用。 But this won't update the resource CoreData (to which your dataGrid is actually binded to) defined in XAML. 但这不会更新XAML中定义的资源CoreData (您的dataGrid实际绑定到该资源

You need to add the object in CoreData list for GUI to get notification which can be done like this: 您需要在CoreData列表中为GUI添加对象以获得通知,可以这样进行:

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

Note - However, ideal sceanario would be to declare list instance in ViewModel and bind Source of CollectionViewSource to that list. 注意 -但是,理想的方案是在ViewModel中声明列表实例,并将CollectionViewSource的Source绑定到该列表。 So, when you modify that list GUI gets notified and you don't have to fetch instance from XAML resources. 因此,当您修改该列表时,GUI会收到通知,而您不必从XAML资源中获取实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM