从 ObservableCollection 到 DataGrid (WPF) 的简单数据绑定?
我有一个“ObservableCollection”,它存储需要粘贴到 DataGrid 中的格式化 ColumnLabels。
该集合可能有许多不同的条目,并且大小可以为“N”,因此我的 DataGrid 中将有“N”个列。定义如下:
private ObservableCollection<string> _stockColumnLabels;
public ObservableCollection<string> StockColumnLabels
{
get
{
if (_stockColumnLabels == null)
{
_stockColumnLabels = new ObservableCollection<string>();
return _stockColumnLabels;
}
return _stockColumnLabels;
}
set
{
_stockColumnLabels = value;
//OnPropertyChanged("StockColumnLabels");
}
}
该集合将保存的示例数据,例如:
- “1”
- “2”
- “3”
- “4”
- “5”
NB,当然,所有字符串基本上都可以是在 GUI 上查看时,将其视为代表列的单行数据。
为了数据绑定,该集合被包装到一个属性中,并且存在于 ViewModel 中,而 DataGrid 则存在于 View 中。当 App.cs 文件运行时,视图的 DataContext 在运行时设置!
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
MainWindow window = new MainWindow();
window.DataContext = new FSEnquiryViewModel();
window.Show();
}
}
GUI (VIEW)
目前,由于该项目所设置的要求的性质,列名称必须根据 ProductCode 进行格式化,以便让生活更轻松,我创建了一个 DataGrid,它只包含一个单行数据是列名称,然后下面的另一个 DataGrid 将保存行值。不要担心第二个 DataGrid,我稍后会对其进行排序,因为设置完全相同。现在有人可以帮助我将 ViewModel 中找到的 ObservableCollection“绑定”到我的 DataGrid,它只是一条数据。
看起来像
1 2 ... n <- 'DataGrid1'
Row1 | 1 | 3 | 5 | 6 | <- 'DataGrid2'
Row2 | 3 | 2 | 1 | 8 |
I have an 'ObservableCollection' that stores formatted ColumnLabels that need pasting into DataGrid.
The collection could have numerous different entries and can be of size 'N' therefore there will be 'N' number of columns in my DataGrid. Defined below:
private ObservableCollection<string> _stockColumnLabels;
public ObservableCollection<string> StockColumnLabels
{
get
{
if (_stockColumnLabels == null)
{
_stockColumnLabels = new ObservableCollection<string>();
return _stockColumnLabels;
}
return _stockColumnLabels;
}
set
{
_stockColumnLabels = value;
//OnPropertyChanged("StockColumnLabels");
}
}
Sample data this collection will hold e.g:
- "1"
- "2"
- "3"
- "4"
- "5"
N.B, all strings of course, essentially can be seen as a single row of data that represent columns when viewed on the GUI.
The collection is wrapped into a property for the sake of DataBinding and lives in the ViewModel and the DataGrid lives in the View. The DataContext of the View is set at RunTime when the App.cs file is run !
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
MainWindow window = new MainWindow();
window.DataContext = new FSEnquiryViewModel();
window.Show();
}
}
GUI (VIEW)
At present due to the nature of the requirements being set on this project, the Column names have to be formatted out of ProductCode so to make life easier, I created one DataGrid that simply holds a single row of data which are column names and then another DataGrid just below that will hold row values. Don't worry about the second DataGrid, I will sort that later, as the set-up is exactly the same. For now can someone help me 'Bind' my ObservableCollection found in my ViewModel to my DataGrid, it's just a single strip of Data.
To Look Like
1 2 ... n <- 'DataGrid1'
Row1 | 1 | 3 | 5 | 6 | <- 'DataGrid2'
Row2 | 3 | 2 | 1 | 8 |
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我下面的代码希望回答允许您绑定来自未知数量的源的数据的问题,每个结果的大小以及可以使用的潜在列数可能未知。然后慢慢地将结果逐行整形为代表最终数据布局的动态集合。
基本上与 BlindMeis 的答案大致相同,但他发布的代码在语法上不正确,并且看起来在发布之前没有经过测试。然而,我的下面是我当前正在使用的代码。
假设您首先有一个或多个结果集,您将在下面找到对我有用的内容。
声明一个表来保存您的结果。
DataTable ShapeResultsTable = new DataTable();
接下来的事情是过滤来自数据库调用或过程的一些结果,您试图重新调整其结果。像这样的东西:
获得列名称列表后,将它们添加到上面声明的数据表中。
接下来,声明、实例化并填充未知大小的列表,因为您的结果的大小为 n 或数量为 n,以存储行值。
接下来,对要在新的形状结果数据表中显示的每一行数据重复步骤 2 和 3,对结果集中找到的每一行重复步骤 2 和 3。
一旦您有了一些代表您想要添加到数据表中的每一行的集合。按照您希望它们显示的顺序添加它们。
shapeResultsTable.Rows.Add(totalsArray.ToArray());
ShapeResultsTable.Rows.Add(branchRowValues.ToArray());
最后,将您的 DataTable 分配给“DataBinded 属性”。
StockResultsTable = ShapeResultsTable;
绑定的属性可能如下所示:
最后,前端的 xaml 可能如下所示:
<前><代码>
我不知道为什么要打开自动生成列属性,但这对我有用。< /em>
步骤 1-9 对我有用,处理从查询或 SProc 返回的结果的示例数据集,上面的步骤简要概述了我在显示来自必须运行的大量查询的结果时所做的操作,以获得非常不同的结果来自许多不同 SPrc 的数据,但具有共同的基础主题这允许它们显示在公共列名称下。
请注意,如果添加一行数据,其中的成员数多于 DataTable 的列数,您将会遇到麻烦。 DT 有 6 列,但您添加的行完全来自不同的查询,该查询有 7 个单独的数据单元格。这不起作用,只需添加尚不存在的新列,然后重试。
My code below hopes to answer the question that allows you to bind data from an unknown number of sources, each results could be unknown in size as well as the potential number of columns that could be used. Then shape the results slowly row by row into a dynamic collection which represents the final data layout.
Basically more or less the same as BlindMeis answer but he posted code that was syntactially incorrect and didn't look like it had been tested before posting. Mine below is however code that I am currently using.
Below you will find what worked for me assuming you have a result set or sets to begin with.
Declare a table to hold your results.
DataTable shapedResultsTable = new DataTable();
Next thing, would be to filter some results from a DB call or procedure who results you are trying to re-shape. Something like:
Once you have a list of column names, add them to the your data table declare above.
Next, declare, instantiate and populate a list of unknown size because your results are of size n or amount n, to store your row values.
Next, repeat step 2 and 3 for every row of data you would like to show on your new Shaped Results Data Table, more over one for each row found in your result set.
Once you have a few collections that represent each row you want to add to your Data Table. Add them in the order you would want them to be displayed in.
shapedResultsTable.Rows.Add(totalsArray.ToArray());
shapedResultsTable.Rows.Add(branchRowValues.ToArray());
Finally, assign your DataTable to a 'DataBinded Property'.
StockResultsTable = shapedResultsTable;
The property that is bound could look like:
Finally the xaml on the front end could look like:
I don't know why the auto generate columns property is turned on but this works for me.
Steps 1-9 worked for me, working with a sample data set of results returned from a query or SProc, the steps above briefly outline what I did in displaying the results from the numerous queries I had to run to get very different data from many different SProcs but had a common underlying theme which allowed them to be displayed under common column names.
Be mindful of the fact that you will run into trouble where adding a row of data which has more members in it that the DataTable has columns. A DT which has 6 columns but you are adding a row which has come from a different Query altogether, which has 7 individual cells of data. This will not work, simply add in the new column that is not already there and try again.
您的列标题集合与“数据”集合的大小相同吗?如果是这样,您可以在 InializeComponent() 之后执行此操作。 (注意:它可能会违反一些 MVVM 规则,但这取决于您想要的严格程度)
Is your column header collection the same size as the "data" collection? If so, you can do this after the InializeComponent(). (Note: it might break some MVVM rules, but it depends on how strict you want to go)
编辑:数据表工作完美。
只需使用 ViewTable 即可构建可供视图使用的集合。我假设您的标头集合与数据的大小相同。
然后将您以前定义的列的数据以及
具有viewmodel 属性
在 xaml 中
绑定的尽可能多的数据行放入您的视图数据网格中,您不必在视图数据网格中执行任何操作,所有信息都来自 viewmodel。
EDIT: DataTable works perfectly.
Simply use a ViewTable to build your collection ready for the view. i assume that your header collection has the same size as the data.
then put in your data for your former defined columns and as much rows of data you have
viewmodel property
binding in xaml
you dont have to do anything in your view datagrid, all information comes from the viewmodel.