问题描述
您好,
我是datagrid的比赛,我使用了xaml的这一部分:
I the contest of datagrid, i use offen this part of xaml:
<Datagrid>
< DataGrid.Columns>
<DataGrid.Columns>
...
...
< DataGridTemplateColumn标头= QUOT;数据">
< DataGridTemplateColumn.CellTemplate>
< DataTemplate>
< TextBlock Text =" {Binding dtt_Protocollo,StringFormat = \ {0:dd / MM / yy\}}" />
< / DataTemplate>
< /DataGridTemplateColumn.CellTemplate>
< DataGridTemplateColumn.CellEditingTemplate>
< DataTemplate>
< DatePicker SelectedDate =" {Binding dtt_Protocollo}" />
< / DataTemplate>
< /DataGridTemplateColumn.CellEditingTemplate>
< / DataGridTemplateColumn>
<DataGridTemplateColumn Header="Data"> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <TextBlock Text="{Binding dtt_Protocollo, StringFormat=\{0:dd/MM/yy\}}" /> </DataTemplate> </DataGridTemplateColumn.CellTemplate> <DataGridTemplateColumn.CellEditingTemplate> <DataTemplate> <DatePicker SelectedDate="{Binding dtt_Protocollo}" /> </DataTemplate> </DataGridTemplateColumn.CellEditingTemplate> </DataGridTemplateColumn>
...
...
< /DataGrid.Columns>
</DataGrid.Columns>
我想插入" DataGridTemplateColumn "在包含或自定义控件中更简单地调用它而不必重写所有代码。
I would like to insert "DataGridTemplateColumn" in an inclusion or custom control to recall it more simply without having to rewrite all the code.
<Datagrid>
<DataGrid.Columns>
...
...
<mycustomtemplatecolumn header="Data" binding="dtt_Protocollo">
...
...
</DataGrid.Columns>
</DataGrid>
</Grid>
我怎么做?
谢谢。
推荐答案
首先用一把钥匙在资源(字典)中定义你的DataGridTemplateColumn。
First you define your DataGridTemplateColumn in resources (dictionary) with a key.
<DataGridTextColumn x:Key="Amount" Header="Amount" Binding="{Binding Amount}"></DataGridTextColumn>
<DataGridTextColumn x:Key="Description" Header="Description" Binding="{Binding theName}"></DataGridTextColumn>
我在我的示例中使用了DataGridTextColumn,但您可以使用模板列。
I used DataGridTextColumn in my example but you can use template column instead.
然后,不是使用定义,而是执行以下操作:
Then rather than using the a definition you would do the following:
<DataGrid Grid.Row="0" DataContext="{StaticResource tvm1}" ItemsSource="{Binding theData}" CanUserAddRows="False" AutoGenerateColumns="False">
<DataGrid.Columns>
<StaticResource ResourceKey="Amount"></StaticResource>
<StaticResource ResourceKey="Description"></StaticResource>
</DataGrid.Columns>
</DataGrid>
这篇关于像函数一样重复部分xaml代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!