WPF DataTemplate in resource, binding

半腔热情 提交于 2021-02-19 05:53:18

问题


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?


回答1:


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>



回答2:


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.



来源:https://stackoverflow.com/questions/38124141/wpf-datatemplate-in-resource-binding

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!