I try a proof of concept. I have an app that it composed of a window and a UserControl that uses the wpfgrid. the window create an instance of the userControl when conditions are met. The user control is binded in code behind . The grid datasource is binded dynamically with a DataGridCollectionView. then a column with button is created using a template define as resource in the xaml part of the userControl.
private void AddColumn()
{
string label = "MyNewColumn";
CellEditor buttonCellEditor = new CellEditor();
buttonCellEditor.EditTemplate = (DataTemplate)Resources["MyTemplate"];
Column column = new Column();
column.FieldName = "id";
column.Title = label;
column.CellEditor = buttonCellEditor;
column.CellContentTemplate = buttonCellEditor.EditTemplate;
column.VisiblePosition = 0;
MyGrid.Columns.Add(column);
}
<Style TargetType="{x:Type Button}" x:Key="MyStyle">
<EventSetter Event="Click" Handler="Button_Click"/>
</Style>
<DataTemplate x:Key="MyTemplate">
<Button Style="{StaticResource MyStyle}" Content="Click Me" ></Button>
</DataTemplate>
---------
internal void Button_Click(object sender, RoutedEventArgs e)
{
int rowIndex = (((System.Windows.Data.CollectionView)(MyGrid.ItemsSource))).CurrentPosition;
//do something
}
the Grid is displayed properly, but the CurrentPosition always = 0, no matter which row is clicked.
Any clue?
Thanks