0

The following question basically exactly describes my problem. I need some new takes on this.

In the question, they say DataTemplateSelector could work for this, but it could only automate the template selection logic, it doesn't help with the data binding issue. The example here is just extremely bad code by the way. (really, looking up the resource in the current main window? it should be looked up next to the element.)

The question rephrased: how do I share a single template that's in resources across multiple columns of a data grid, with databinding support, that is, I want to be able to specify, at the data column level, to which property the control in the instantiated template should bind, without copy-pasting the template definition in each of the columns?

I have read the answers here and here, I have tried implementing a solution both with and without the rerouting via a custom binding attached property implemented in the second question, both with and without a nested template definition. When I'm trying to access RelativeSource to reach out for the attached property on the parent, it says Binding.RelativeSource cannot be set while using Binding.StaticSource. This might be because the questions use WPF Toolkit, while I'm using the default data grid controls. My markup, my attached property definition. To be clear, if I do it the dumb, copy-paste way, the cells show the date picker perfectly fine.

It would've been awesome to be able to do something like that, but I failed to make it work so far:

<Grid.Resources>
    <DataTemplate x:Key="datePicker">
        <StackPanel>
            <DatePicker SelectedDate="{Binding ???}"/>
        </StackPanel>
    </DataTemplate>
</Grid.Resources>
<DataGrid Name="CarDataGrid" AutoGenerateColumns="false">
    <DataGrid.Columns>
        <DataGridTemplateColumn
            Header="ManufacturedOn"
            ???="{Binding Path=ManufacturedDate}"
            CellTemplate="{StaticResource datePicker}"/>
    </DataGrid.Columns>
</DataGrid>
  • https://stackoverflow.com/questions/17381753/wpf-datagrid-creating-a-new-custom-column – ASh Oct 10 '22 at 14:15
  • @ASh, are you saying you can't do this with templates? The link you sent has a custom column implementation. Is there maybe a way to combine the custom implementation , the template and the binding? – Anton Curmanschii Oct 10 '22 at 14:37
  • I think that I personally can do this with templates. But it would take me a few hours of work, and before going that way I would try to implement custom column type – ASh Oct 10 '22 at 15:07

0 Answers0