How to extend behavior of Templated Control with children #18186
-
I've read the existing tutorial for creating Templated Control and discussion in #2516, but still am not able to figure out how to resolve my issue and need assistance. I have to extend behaviour of the DataGrid control, no changes in look and feel. just to override some functions and do extra stuff on some events. My first approach was to create a new UserControl that inherits from the original DataGrid, but than realised that this was a wrong approach, as DataGrid is a TemplatedControl. I don't need to wrap DataGrid into any new parent control, so I decided to try to work without XML templates using just direct instancing. I inherited my new control DataGridEx from the original DataGrid and defined StyleKeyOverride to the base class: public class DataGridEx : DataGrid
{
protected override Type StyleKeyOverride => typeof(DataGrid);
} And I inserted my new control into MainWindowViewModel xaml : <local:DataGridEx ItemsSource="{Binding Items}">
<local:DataGridEx.Columns>
<DataGridTextColumn Header="Value 1"
Binding="{Binding Value_1}"/> />
<DataGridTextColumn Header="Value 2"
Binding="{Binding Value_2}"/>
...
</local:DataGridEx.Columns>
</local:DataGridEx> Build fails trying to resolve property Could you please tell what I have to do to avoid this error? And what is the correct approach for extending TemplatedControls in case if only code behind should be overriden? public class MainWindowViewModel
{
public ObservableCollection<RowViewModel> Items { get; }
...
}
public class RowViewModel
{
public string? Value_1 { get; set;}
public string? Value_2 { get; set; }
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
It seems compiled doesn't respect AncestorType-delivered types and only expects exact match. |
Beta Was this translation helpful? Give feedback.
x:DataType
is implicitly assumed from theDataGrid.ItemsSource
thanks to this attribute defined on the base column class: https://github.com/AvaloniaUI/Avalonia/blob/master/src/Avalonia.Controls.DataGrid/DataGridBoundColumn.cs#L28It seems compiled doesn't respect AncestorType-delivered types and only expects exact match.
As a workaround, I would recommend explicitly setting x:DataType on these columns.