2

Let's assume that you have

  • an array of N integers
  • an integer value representing number of rows

in the model. The integer is bound to a ComboBox in the view.

Q1) How do you bind the array (or individual items of the array) to a DataGrid or a ListView control so that:

  • when you change the ComboBox value, only that many rows are visible/generated in the DataGrid or ListView
  • Individual rows of the DataGrid or ListView contain TextBoxes (or similar) that allow to edit the corresponding value in the array. The binding must be two-way.

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) How would you provide another column (only text information) to the DataGrid / ListView so that:

  • First row would always read 0. Each subsequent row would read: 'previous row' + '360 divided by ComboBox selected value' (providing that it would be limited to even numbers for simplicity).

Any help or suggestions are really appreciated.
Thank you.

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.

3
  • I'd like to direct your attention to the Search and Research section of the Help Pages on How to ask a good question. You're supposed to show us what you have tried and explain why it didn't work... a question like yours currently is makes it appear that you are asking us to do your work for you. Commented Nov 19, 2013 at 9:41
  • Word's RAND() function makes more sense than this question. Commented Nov 19, 2013 at 11:23
  • @dev hedgehog Could you please take a look here and perhaps ask what you don't understand? I am simply trying to create (in a nutshell) a DataGrid/ListView that dynamically expands/shrinks depending on selected ComboBox value. Has two columns - 2nd has TextBoxes bound to values of an array. 1st one has text information computed based on selected ComboBox value. Thank you in advance. Commented Nov 19, 2013 at 11:34

1 Answer 1

1

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.

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}" />

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

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you very much for you answer, it was spot on and very well presented. This is exactly what I was looking for and shows that you have quite some experience under you belt. Thanks :).
Question: Usually a property in ViewModel also updates the value in the underlying model. In case of this datagrid I have the values entered into datagrid stored in VM exclusively (they don't propagate into model automatically). Is it a common practice to have a method that writes the data to the model, and have it called when the code returns from displaying the dialog window? What approach would you recommend? Shall I open a question for this?
@kajovajo, yes it's probably best to ask a new question for this... I'll look out for it, or if you mention '@Sheridan' somewhere in it or one of its comments, I'll get notified by StackOverflow. Please add as much detail as you can and can you also please clarify what you mean by model... this can unfortunately be used to mean either the data type classes or the means of storing the data in the database.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.