I'm using a DataGrid in my .NET 8 WPF app. The datagrid is set to a data context from a third-party library. I'd like to add a button column (as a DatagridTemplateColumn) to call a function in my own view model. I can't seem to figure out how to make this work.
I tried this:
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="Map" DataContext="viewmodels:vmView1" Command="{Binding OpenMapCommand}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
In my view model vmView1:
public ICommand OpenMapCommand { get; private set; }
public vmView1()
{
OpenMapCommand = new RelayCommand<object>(OpenMap);
}
Is this even possible?
Thanks.
Don
RelativeSourceon the binding to find theDataContextof theWindowSee here for more details. stackoverflow.com/questions/84278/…<Button Content="Map" Command="{Binding Path=DataContext.OpenMapCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type viewmodels:vmView1}}}"/>Autocomplete finds the OpenMapCommand, but when I run it, I get a binding error because the data context is null. So I'm still missing something.