I have a simple WPF window with a Datagrid and an "Add" button. The add button is wired to the ViewModel using a RelayCommand, and basically just adds a new object to the observable collection and then sets the selected item (also databound from the grid's SelectedItem property). The end result is a new row is added to the grid and is then set to the selected row. This is almost the result I'm trying to achieve, because the next step is to put the grid in edit mode by invoking BeginEdit. The problem is, from my understanding, the VM isn't supposed to access controls on the view directly.
So my question is, what would be the most "correct" way of achieving this using MVVM?
Related
I have an ItemsControl binding to a collection in the ViewModel. As a result of user input, a new item gets added to the collection and this gets displayed on the View.
The item is also a View-ViewModel pair, the View contains a TextBox that I would like to receive focus immediately after being added to the collection.
How do I set the focus to a TextBox without referencing the View from within the ViewModel? Are attached properties the way to go here?
well you can do this by creating a behaviour..
have a look here to get an idea of behaviours controlling focus
I have a UserControl in WPF and it contains a variety of controls in it... The most important being the DataGrid Control. The DataGrid control is bound to an Observable Collection list. This list filled with different items based on couple of filters selected by the user. Now, once the DataGrid displays all the data. I want to set the Focus on the first (Filters) combobox on my usercontrol. Is there any way to know when the DataGrid has completely loaded??? Is there any DataTrigger I could set that i could trigger to inform me that Grid was refreshed with new list and I can set the ComboBox to focus. Basically i'm not able to set the focus to the first control on my UserControl when the data in the DataGrid has been repopulated... Please let me know if anyone knows how to resolve this issue!!
Thanks in advance.
I hate to work in code behind but sometimes that is only way. This can be done in two ways according to me:
Use EventAggregator to raise the event from your ViewModel once you are done with your filtering logic in it. Listen to this event in view code behind and in the handler do firstCombo.Focus()
Second is bit dirty, So there must be the button or the last control after which you apply filter on your listview. In the buttonClickHandler/or any event of the last control (like selectionchange) directly do firstCombo.Focus(). In this case the moment you will press your button focus will move to combo.
Thanks
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.
The ASP.NET GridView (and other controls) has the very handy DataBound event, which fires after the GridView finishes databinding. Is there an equivalent event for the Silverlight ListBox (WP7.1)?
My ListBox changes constantly based on user input and I would like to scroll the listbox to a certain item.
As far as I know there is no such event.
But you could use a more generic change listener like ItemsChanged:
listBox1.ItemContainerGenerator.ItemsChanged += new ItemsChangedEventHandler(ItemContainerGenerator_ItemsChanged);
This will also react on normal list changes but you can easily filter these.
The ListBox has a SelectedItem property. Just set it to one of the items, or set the SelectedValue and that will automatically scroll it to make visible.
Here is the MSDN reference of that property: http://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.selecteditem.aspx
I am working on WPF datagrid control. Binding the data from code behind using a dataset/XML. I want to edit the cell of the grid and capture the new value which the user has entered. Is there a code sample to show how to do that?
What event should I use and which property of datagrid (if any) should I use, selecteditem or selectedcell?
(I am kind of new to WPF datagrid control, so I apologize if this is kind of kiddish...)
You can use CellEditEnding and RowEditEnding. You can access the respective row and its Item from the event arguments.