C
C#2y ago
Anton

WPF passing binding to a template from DataGridTemplateColumn

Say, I have a template like this in resources, which I want to reuse for multiple columns, and which could be way more complicated:
<DataTemplate x:Key="datePicker">
<DatePicker SelectedDate="{Binding ??}"/>
</DataTemplate>
<DataTemplate x:Key="datePicker">
<DatePicker SelectedDate="{Binding ??}"/>
</DataTemplate>
Then, I use DataGridTemplateColumn to define a column that should use the template:
<DataGrid.Columns>
<DataGridTemplateColumn
Header="ManufacturedOn"
CellTemplate="{StaticResource datePicker}"
??="{Binding ManufacturedDate}" />
</DataGrid.Columns>
<DataGrid.Columns>
<DataGridTemplateColumn
Header="ManufacturedOn"
CellTemplate="{StaticResource datePicker}"
??="{Binding ManufacturedDate}" />
</DataGrid.Columns>
The question is how to pass the binding to the template? Absolutely all examples that I find on the Internet just define the template within the column, so if they had two columns of the same type, they would define two exactly identical templates, where only the binding path would differ. It is possible to achieve it via the Tag property or with custom properties, as a guy does here https://stackoverflow.com/a/7310771/9731532 but there the context is a source of an image. I was wondering if a property for my use case is already defined for DataGridTemplateColumn.
Stack Overflow
Pass parameter to a customized template
I've edited a template from a checkbox, then I added an image into it without defining its "Source" property. Style : <Style x:Key="ImageCheckbox" TargetType="{x:Type CheckBox}"> <
1 Reply
Anton
Anton2y ago
how do I pass a binding object via a property anyway? is that even possible?