WPF 重用 DataGrid 模板Column DataTemplates
我已经构建了一个具有自定义列的数据网格:
<DataGridTemplateColumn
Header="{x:Static local:MainWindowResources.gasNameLabel}"
Width="*"
MinWidth="150">
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<TextBox
Name="GasNameTextBox"
Text="{Binding Path=Name, UpdateSourceTrigger=PropertyChanged}"
Padding="2,0,0,0" />
<DataTemplate.Triggers>
<Trigger
SourceName="GasNameTextBox"
Property="IsVisible"
Value="True">
<Setter
TargetName="GasNameTextBox"
Property="FocusManager.FocusedElement"
Value="{Binding ElementName=GasNameTextBox}"/>
</Trigger>
</DataTemplate.Triggers>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Label
Name="GasNameLabel"
Content="{Binding Path=Name}"
Padding="0,0,0,0"
Margin="6,2,2,2" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
由于我将大量重复使用此类列定义,因此我真的很想将其定义为外部 DataTemplate,我只提供要绑定的属性(Binding Path= ...)并且其余部分被重用...这样我将定义文本模板、复选框模板等,并在各种网格中重用它们,并且仅更改对不同属性的绑定。
这可能吗?
弗拉丹
I've build a datagrid which has a custom column:
<DataGridTemplateColumn
Header="{x:Static local:MainWindowResources.gasNameLabel}"
Width="*"
MinWidth="150">
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<TextBox
Name="GasNameTextBox"
Text="{Binding Path=Name, UpdateSourceTrigger=PropertyChanged}"
Padding="2,0,0,0" />
<DataTemplate.Triggers>
<Trigger
SourceName="GasNameTextBox"
Property="IsVisible"
Value="True">
<Setter
TargetName="GasNameTextBox"
Property="FocusManager.FocusedElement"
Value="{Binding ElementName=GasNameTextBox}"/>
</Trigger>
</DataTemplate.Triggers>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Label
Name="GasNameLabel"
Content="{Binding Path=Name}"
Padding="0,0,0,0"
Margin="6,2,2,2" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
As I am going to be reusing such column definition A LOT, I would really like to define it as an external DataTemplate to which I only provide the property to bind on (Binding Path= ...) and that the rest is reused...that way I would define Text template, Checkbox template and such and reuse them in various grids and only change bindings to different properties.
Is that possible?
Vladan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 UserControl 代替,将其放入您的应用程序资源,并重用用户控件。
Use a UserControl instead, put that in your application resources, and reuse the usercontrol.