[英]Binding DataTemplate content
I have the following DataTemplate
: 我有以下DataTemplate
:
<DataTemplate x:Key="ButtonTemplate">
<Button Click="Cell_Click">
<Button.Template>
<ControlTemplate TargetType="{x:Type Button}">
<TextBlock x:Name="TBlock" />
</ControlTemplate>
</Button.Template>
</Button>
</DataTemplate>
And I use this DataTemplate
as GridViewColumn.CellTemplate
, but I have to bind dynamically the Text
Property of the TextBlock
when I add a new column: 而且我将此DataTemplate
用作GridViewColumn.CellTemplate
,但添加新列时必须动态绑定TextBlock
的Text
属性:
GridViewColumn column = new GridViewColumn();
column.CellTemplate = Resources["ButtonTemplate"] as DataTemplate;
// How to get the TBlock (TextBlock) of CellTemplate and bind its property here?
MyGridView.Columns.Add(column);
What have I to do? 我该怎么办 Thanks. 谢谢。
您可以为TextBlock的Loaded-Event附加一个EventHandler并在那里创建绑定(如果足够的话,则基于TextBlock的DataContext)。
忘记修改模板,最好在代码中从头开始创建模板(或者可以使用XamlReader.Parse
和XAML字符串以及绑定代码动态插入)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.