problem with wpf command not executing when button clicked
Looks like you are setting the DataContext
of the Grid
to the Data
property of your ViewModel (or object). If the object that the Data property exposes doesn't provide the SaveData
command, you'll have the problem you're describing. Remember the DataContext
is inherited from the parent.
If you require that the DataContext
is set in that manner, and still require the button to reference the parent DataContext
, one option would be to use a RelativeSource to point to an element that has the ViewModel as the DataContext
.
In WPF you also have the option of making those commands static and using the {x:Static}
markup extension to reach it.
Hope that helps.
EDIT: Here's an example if your <Grid>
is contained in a <UserControl>
.
<Button Command="{Binding Path=DataContext.SaveData,
RelativeSource={RelativeSource Mode=FindAncestor,
AncestorType={x:Type UserControl}}}" ... />
Also, I don't know what your full XAML looks like, but I suspect that this can be simplified greatly by removing the DataContext
on the Grid
and Binding Data on the ItemsControl
(or whatever you're using to show the list of objects).