【发布时间】:2015-06-04 15:36:21
【问题描述】:
我有这样的模板
<Window.Resources>
<DataTemplate x:Key="MemberCoefDataTemplate">
<StackPanel>
<CheckBox Name="CheckBox"></CheckBox>
<TextBox Name="TextBox"></TextBox>
</StackPanel>
</DataTemplate>
</Window.Resources>
我在网格中使用它
<DataGridTemplateColumn CellTemplate="{StaticResource MemberCoefDataTemplate}" />
我需要动态地将列添加到网格中。作为绑定,我使用索引器属性。所以我需要动态设置绑定,因为我不知道绑定到哪个索引。当我只是为了测试尝试过这个
var column = new DataGridTemplateColumn();
column.CellTemplate = (DataTemplate)Application.Current.MainWindow.Resources["MemberCoefDataTemplate"];
TextBox tb = column.CellTemplate.FindName("TextBox", dg) as TextBox;
我得到了 InvalidOperationException 和描述:
此操作仅对应用了此模板的元素有效
【问题讨论】:
标签: wpf binding datatemplate code-behind