I am working on a silverlight application using MVVM. My requirements is to display existing user data in data grid so that first two columns remains non-editable and rest will be editable.
At start datagrid loads data from database, at that point if user click on data grid first two columns should be non-editable.
After that user insert a new row (i create a button, when that is clicked a new row is added at the bottom of the grid) all columns should be editable including first two. Now user can click Add row buttons more than once, point is rows created by Add button click should be editable.
I am stuck at this problem since yesterday any help would be great!
I dont know Silverlight but I think the following should work:
On your command to add a new row, set a flag like "AllRowsEditable" to true and throw a PropertyChanged for this Property. In your view you bind the IsReadonly property of the first two columns to that "AllRowsEditable" property.
EDIT: Write a ViewModel for your DataGrid items. For example "RowViewModel". To have a good structure I would introduce two properties like "IsFirstPropertyReadOnly" and "IsSecondPropertyReadOnly" in that ViewModel. "...firstProperty..." is your properties name. In your XAML you can bind to this properties. In your first initialization you load the items and set the property values to true. All items added after that you set that properties to false.
Related
I have a WPF DataGrid with a few radio button columns and a couple of textbox columns.
All columns are DataGridTemplateColumns so I can customize the string display formatting. (Not sure if this has any bearing on anything.)
There are two columns with radio buttons that are supposed to be grouped together to allow the user to select one, or the other.
In the view model behind a row, there is code that generates a unique groupname for the pair of radio buttons so that each row has its own set of mutually exclusive options.
The problem is, it seems that the DataGrid doesn't instantiate the row's viewmodel until a textbox is activated. That's when the constructor is triggered. Until that point, there is no unique groupname so a user can select both radio buttons. Once the constructor runs, the radio buttons behave properly.
Is there a way to force the viewmodel to be instantiated as soon as the DataGrid displays the new row?
The radio buttons should have a command that you can bind to - allowing you to programmatically instantiate the view model. You could send the datacontext of the row as the parameter of the command (this may even force instantiation), and then you could instantiate it and add it to the collection that the DataGrid is using as a DataSource if it is still null.
I have bound a WPF4 DataGrid to an ObervableCollection in the conventional way. I can add to the collection and get a new row in my grid. All good. But, how can I know when the new row has been added and I can do something with it? I want to set the focus to a particular cell in the new row and make it editable. There seems to be no event along the lines of "NewRowAddedAndYouCanDoSomethingWithIt".
Edit 11:39 4 Dec:
Thanks all for your reponses. Despite the lectures on what to do or not I'm still puzzling about how to achieve this. I add to the ObervableCollection and get a new row that the user can edit but I want to avoid the user needing to click in the new row. I want the cursor in a specific cell in edit mode once a new row is added in the DataGrid.
This seems like a strange question... you say that you bound a WPF4 DataGrid to an ObervableCollection in the conventional way and that you can add to the collection and get a new row in my grid. But your question is How can I know when a row has been added?
You can know when a row has been added... because when you add an item to the collection... the Framework will add a row to the DataGrid.
Furthermore, if you really were using MVVM correctly, then you wouldn't care when the UI row is actually added, because when using WPF and MVVM, we manipulate data, not UI elements. In that case, you would bind some bool property to the IsFocused property and set it from your view model instead.
in addition to the answer and comments:
if you add a new row to your collection
var myNewRow = new MyObject(){Name="its New"};
this.MyCollection.Add(myNewRow);//<-- this new row is added to you collection, and your datagrid will show the new row
myNewRow.Name = "Test1";//if you implment INotifyPropertyChanged in your MyObject, the new Value its displayed in the grid
you never need an event when the new row is added to the grid in this case. but maybe i miss something, so let my know if you have other requirements.
I have a ListBox in a Silverlight Application. I'm trying to make an editable listbox, so I use an ItemTemplate to have the controls i need in each item, like a textBox and buttons, and its working fine.
I'd like to have a line at the end of the Listbox with a button to add new items. Since this item won't be related to any of my domain classes, I'm using a plain object as a 'Filler', and then I have code that identify this item to show the button correctly.
myListBox.Items.add(new object());
The problem is that I want this "new Record" item to be kept always at the end of listbox, so when I need to insert a new domain record, i use this code:
myListBox.Items.Insert(myListBox.Items.Count - 1, domainItem);
When I debug the myListBox.Items collections, it is in the right order, with the "add new" button at the end, but the listbox is displaying this button at the beggining. Why are my items beeing displayed in a different order than the Items collection?
Unless there is a specific reason, instead of trying to put the button into the listbox collection itself you would be best to create a new control with the button outside and below the listbox. You can always style the button to look as though it's within the listbox if required.
Is there a reason why you are not using a datagrid because it would remove all your ordering problems and it would allow you to edit the entries.
The datagrid is bound to an ObservableCollection which automatically connects your editable fields to the GUI.
Cheers,
I need a control that can list data in 3 columns, I want to manually add the items (not databind the control).....
firstly I'll populate column 1 and 2, then later when a button is clicked, I need to loop through the values of column 1 and 2, perform an operation, then update the value of column 3.
From what I remember the datagrid does not allow for this , and must be bound to a data source, am I right?
What is the best quick drag and drop control I can use from the toolbox?
Use a ListView with View property set to Details. Then you can add columns to the listview.
From what I remember the datagrid does
not allow for this , and must be bound
to a data source, am I right?
This is not right. The DataGrid can be used in bound or unbound mode. If you don't bind it, you can just add rows and columns and populate cell contents as you need. You could choose to do this if you wish. It will solve your problem.
But I think it would be simpler for you to create a custom class to represent a row and use the DataGrid in bound mode. You can refresh the binding when you click the button.
I'm using the WPF DataGrid, and I'd like to know if there is any way I can access the DataGridRow's RowDetails programatically.
For example, when the user selects the row, I'd to grab some data from somewhere (say, a database), and display it in the RowDetails.
All of the examples I've seen tend to just display some extra bound data that isn't included in the columns for the row.
You could display some extra data but lazy-load it on SelectionChanged.
It usually is not easy to work directly with the WPF controls, they are not really meant to be used without a backing databound model.
If you have all the data in list of objects (or something similar) then you can do all sorts of fun things. I'm using a WPF Datagrid in this manner, and when a user selects a row, I think populate an editor control above the grid with the row item plus additional details.
That said, there's nothing stopping you from adding in additional information in the grid that's normally hidden and a trigger on row selection to show the additional data
you can use the following code
DataRowView row = (DataRowView)MyDataGrid.SelectedItem;
string strName = row.Row["Name"].ToString(); //where "Name" is the column name