简体   繁体   中英

WPF DataTemplate in resource, binding

I have below DataGrid(simplified)

<DataGrid ItemsSource="{Binding Something}">
    <DataGrid.Columns>
        <DataGridTemplateColumn Header="Test">
            <DataGridTemplateColumn.CellEditingTemplate>
                <DataTemplate>
                    <Grid>
                        <TextBox Text="{Binding A}"/>
                    </Grid>
                </DataTemplate>
            </DataGridTemplateColumn.CellEditingTemplate>
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <Grid>
                        <TextBlock Text="{Binding A}"/>
                    </Grid>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
        <DataGridTemplateColumn Header="Test">
            <DataGridTemplateColumn.CellEditingTemplate>
                <DataTemplate>
                    <Grid>
                        <TextBox Text="{Binding B}"/>
                    </Grid>
                </DataTemplate>
            </DataGridTemplateColumn.CellEditingTemplate>
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <Grid>
                        <TextBlock Text="{Binding B}"/>
                    </Grid>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</Datagrid>

Is there any way to move DataTemplate to resources and reuse it for different properties so I don't have to copy and paste DataTemplate for every property?

A pragmatic solution would be to define templates on resource level and wrap them in a 'ContentPresenter' in each column. You still have to define a template for each column explicitly. Still, you can manage the templates in one place and quickly see to which properties your columns are bound.

<DataGrid>
     <!-- Templates in a single place in resources -->
     <DataGrid.Resources>
        <DataTemplate x:Key="CellTemplate">
           <TextBlock Text="{Binding}" />
        </DataTemplate>
        <DataTemplate x:Key="EditCellTemplate">
           <TextBlock Text="{Binding}" />
        </DataTemplate>
     </DataGrid.Resources>
     <DataGrid.Columns>
        <DataGridTemplateColumn>
           <DataGridTemplateColumn.CellTemplate>
              <DataTemplate>
                 <ContentPresenter Content="{Binding A}" ContentTemplate="{StaticResource CellTemplate}" />
                 </DataTemplate>
              </DataGridTemplateColumn.CellTemplate>
              <DataGridTemplateColumn.CellEditingTemplate>
                 <DataTemplate>
                    <ContentPresenter Content="{Binding A}" ContentTemplate="{StaticResource EditCellTemplate}" />
                 </DataTemplate>
              </DataGridTemplateColumn.CellEditingTemplate>
            </DataGridTemplateColumn>
            <DataGridTemplateColumn>
               <DataGridTemplateColumn.CellTemplate>
                  <DataTemplate>
                     <ContentPresenter Content="{Binding B}" ContentTemplate="{StaticResource CellTemplate}" />
                  </DataTemplate>
               </DataGridTemplateColumn.CellTemplate>
            <DataGridTemplateColumn.CellEditingTemplate>
               <DataTemplate>
                  <ContentPresenter Content="{Binding B}" ContentTemplate="{StaticResource EditCellTemplate}" />
               </DataTemplate>
            </DataGridTemplateColumn.CellEditingTemplate>
         </DataGridTemplateColumn>
      </DataGrid.Columns>
</DataGrid>

You could do it like this:

<DataGrid ItemsSource="{Binding Something}">
      <DataGrid.Resources>
         <DataTemplate x:key="MyTemplate">
             <Grid>
               <TextBox Text="{Binding}"/>
             </Grid>
         </DataTemplate>
      </DataGrid.Resources>
        <DataGrid.Columns>
            <DataGridTemplateColumn Header="Test" 
                                    CellTemplate="{StaticResource MyTemplate}" 
                                    >
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </Datagrid>

But then you need to rethink binding logic, as you will need to use cell datacontext in the same template for all the columns.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM