问题描述
让我们假设你有
- N个整数数组
- 的整数值再行presenting号
在模型中。整数绑定到视图的组合框。
in the model. The integer is bound to a ComboBox in the view.
Q1)如何你阵列(或阵列的单个项目)到DataGrid或ListView控件,这样绑定:
Q1) How do you bind the array (or individual items of the array) to a DataGrid or a ListView control so that:
- 当您更改组合框的值,只有多行可见/在DataGrid或ListView 生成
- 在DataGrid或ListView的各行包含文本框(或类似),允许编辑数组中的对应值。的结合,必须是双向的。
即。如果我在下拉框中选择5,只有5行是包含5文本框被分别绑定到第5个项目的数组可见。
I.e. If i select 5 in the ComboBox, only 5 rows are visible containing 5 TextBoxes that are each bound to the first 5 items of the array.
Q2)你会如何提供另一列(纯文本信息)到DataGrid / ListView中,因此:
Q2) How would you provide another column (only text information) to the DataGrid / ListView so that:
- 第一行始终显示为0。每个接下来的行会读:'previous行+360通过ComboBox中选定值除以(前提是它会被限制在偶数为简单起见)。
任何帮助或建议,真正AP preciated。
谢谢你。
Any help or suggestions are really appreciated.
Thank you.
修改(2013年11月22日):
继Sheridan我链接this问题到有更多的信息(和上下文),对这个问题我的其他问题。
EDIT (22.11.2013):
Following suggestions of Sheridan I am linking this question to my other question that has more information (and context) to this question.
本来我是因为我认为剥夺任何情况下只是为了裸露力学会得到更好的理解和更好的机会,被回答的下降的问题。
的我认错。的
Originally I opened this question because I thought that a question stripped down of any context just to the bare mechanics would get better understanding and better chance to being answered.
I stand corrected.
推荐答案
好了,如果你要正确地做到这一点,您首先需要创建一个数据类型/型号类来保存数据。它应该实现并包含要在显示每个列属性的DataGrid
的包括多一件。你的'Q2的要求的
Ok, so if you're going to do this properly, you'll first need to create a data type/model class to hold your data. It should implement the INotifyPropertyChanged
interface correctly and contain a property for each column that you want to display in the DataGrid
including an extra one for your 'Q2' requirement.
接下来,您要添加类型的两个属性的ObservableCollection< YourDataType>
到后面/视图模型的code。首先将举行全收集第二个会显示您所需的行只是数。您还需要一个整数
属性绑定
到组合框:
Next, you want to add two properties of type
ObservableCollection<YourDataType>
into your code behind/view model. The first will hold the whole collection and the second will display just the number of rows that you want. You'll also need an integer
property to Bind
to the selected item of the ComboBox
:
<DataGrid ItemsSource="{Binding FilteredItems}" ... />
...
<ComboBox ItemsSource="{Binding Numbers}" SelectedItem="{Binding SelectedItem}" />
现在,每当在视图模型中的
的SelectedItem
属性改变,你只需要更新在 FilteredItems 属性:
Now, whenever the
SelectedItem
property changes in the view model, you just need to update the number of rows of items in the FilteredItems
property:
public int SelectedItem
{
get { return selectedItem; }
set
{
selectedItem = value;
NotifyPropertyChanged("SelectedItem");
UpdateFilteredItems();
}
}
...
private void UpdateFilteredItems()
{
FilteredItems =
new ObservableCollection<YourDataType>(Items.Take(SelectedItem));
}
在
UpdateFilteredItems
方法,我们简单地采取项目的相关数量从根据收集全产品
的SelectedItem
值。由于 FilteredItems
收集绑定到 DataGrid.ItemsSource
,用户界面将自动更新。
In the
UpdateFilteredItems
method, we simply take the relevant number of items from the whole Items
collection based on the SelectedItem
value. As the FilteredItems
collection is bound to the DataGrid.ItemsSource
, the UI will automatically update.
这篇关于WPF的DataGrid / ListView控件绑定到数组MVVM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!