Commands in User Control
-
I am having an issue binding a command to a user control. My main window calls a workspace viewmodel that supplies data via observable collection to a datagrid in the workspace viewmodel. In this datagrid I put a button, but am having an issue figuring out how to bind a command to it. The datagrid itemssource is an observable collection from a viewmodel. I have a command on that same view model that tells the datagrid what to do when a button field is pressed. I bound to that name but it isn't working because. I am guessing this is because the datagrid (Which is on a user control) is bound to the observable collection of data and the command is outside the scope of that collection. So on what viewmodel do I put my command in order to execute it on my usercontrol and how to I reference it on my user control? Sorry for the vagueness of it all, this is a bit difficult to explain. Cheers, --EA
-
I am having an issue binding a command to a user control. My main window calls a workspace viewmodel that supplies data via observable collection to a datagrid in the workspace viewmodel. In this datagrid I put a button, but am having an issue figuring out how to bind a command to it. The datagrid itemssource is an observable collection from a viewmodel. I have a command on that same view model that tells the datagrid what to do when a button field is pressed. I bound to that name but it isn't working because. I am guessing this is because the datagrid (Which is on a user control) is bound to the observable collection of data and the command is outside the scope of that collection. So on what viewmodel do I put my command in order to execute it on my usercontrol and how to I reference it on my user control? Sorry for the vagueness of it all, this is a bit difficult to explain. Cheers, --EA
After reading your convoluted question I can sympathise completely. I think, your ITEMSOURCE for the datagrid is the collection, your DATACONTEXT of the control is where the command should reside. This stuff is completely different from what I am used to!
Never underestimate the power of human stupidity RAH
-
I am having an issue binding a command to a user control. My main window calls a workspace viewmodel that supplies data via observable collection to a datagrid in the workspace viewmodel. In this datagrid I put a button, but am having an issue figuring out how to bind a command to it. The datagrid itemssource is an observable collection from a viewmodel. I have a command on that same view model that tells the datagrid what to do when a button field is pressed. I bound to that name but it isn't working because. I am guessing this is because the datagrid (Which is on a user control) is bound to the observable collection of data and the command is outside the scope of that collection. So on what viewmodel do I put my command in order to execute it on my usercontrol and how to I reference it on my user control? Sorry for the vagueness of it all, this is a bit difficult to explain. Cheers, --EA
If you use datagrid and you want a command inside the datagrid, then you should define that command in the ObservableCollection item object. eg, If you use
ObservableCollection<Customer>
as ItemsSource, define the command inCustomer
class.Arun Jacob My Technical Blog : Code.NET
-
I am having an issue binding a command to a user control. My main window calls a workspace viewmodel that supplies data via observable collection to a datagrid in the workspace viewmodel. In this datagrid I put a button, but am having an issue figuring out how to bind a command to it. The datagrid itemssource is an observable collection from a viewmodel. I have a command on that same view model that tells the datagrid what to do when a button field is pressed. I bound to that name but it isn't working because. I am guessing this is because the datagrid (Which is on a user control) is bound to the observable collection of data and the command is outside the scope of that collection. So on what viewmodel do I put my command in order to execute it on my usercontrol and how to I reference it on my user control? Sorry for the vagueness of it all, this is a bit difficult to explain. Cheers, --EA
One way I have found to solve this is to bind your command to the parent datasource in the xaml. For e.g.
cmd:mycommand="{Binding ElementName=LayoutRoot, Path=DataContext.RevertClick}"
.LayoutRoot
is what would be the main layout grid in the xaml (added by default)....but you could have used a different name.The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it. My latest tip/trick Visit the Hindi forum here.
-
If you use datagrid and you want a command inside the datagrid, then you should define that command in the ObservableCollection item object. eg, If you use
ObservableCollection<Customer>
as ItemsSource, define the command inCustomer
class.Arun Jacob My Technical Blog : Code.NET
I had considered that as well, but the class is an entity created by the entity framework directly from an SQL table and I thought that overriding that class to add a command would be a little bit more spaghetti than I wanted to deal with. Still, that seems to me like the best working solution. Thank you for the advice. Cheers, --EA
-
I am having an issue binding a command to a user control. My main window calls a workspace viewmodel that supplies data via observable collection to a datagrid in the workspace viewmodel. In this datagrid I put a button, but am having an issue figuring out how to bind a command to it. The datagrid itemssource is an observable collection from a viewmodel. I have a command on that same view model that tells the datagrid what to do when a button field is pressed. I bound to that name but it isn't working because. I am guessing this is because the datagrid (Which is on a user control) is bound to the observable collection of data and the command is outside the scope of that collection. So on what viewmodel do I put my command in order to execute it on my usercontrol and how to I reference it on my user control? Sorry for the vagueness of it all, this is a bit difficult to explain. Cheers, --EA
Are the columns auto-generated?
-
Are the columns auto-generated?
Not auto generated. The answer was to reference the command on the viewmodel using relativesource - findancestor. Nice and easy. Thanks for the help.