donsay
donsay

Reputation: 11

How to set a different DataContext for a WPF datagrid column?

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

Upvotes: 0

Views: 41

Answers (0)

Related Questions