【发布时间】:2020-12-07 23:01:08
【问题描述】:
为什么不关闭这个问题?
请不要关闭问题:suggested link 不包含答案,因为DataGridTemplateColumn 中没有Binding 属性,我找不到将数据绑定到它的方法。
似乎可以在数据模板中使用Text={Binding},这是答案的“一半”
原始问题
我是 WPF 的新手。
我有一个DataGrid,我想在其中为某些列提供特定模板,但所有模板都是相同的。
例如
<DataGrid ItemsSource="{Binding Items}" AutoGenerateColumns="False" CanUserAddRows="False">
<DataGrid.Columns>
<!-- first column is a standard column, ok-->
<DataGridTextColumn Header="First Column" Binding="{Binding FirstColumn}"/>
<!-- a few of the other columns use a custom template-->
<DataGridTemplateColumn Header="Second Column">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding SecondColumn, UpdateSourceTrigger=PropertyChanged}"/
<OtherThings />
<OtherThings />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<More Columns Exactly like the above, with different bindings/>
<!-- Column3 Binding={Binding Column3}-->
<!-- Column4 Binding={Binding Column4}-->
</DataGrid.Columns>
</DataGrid>
如何以可以设置此模板绑定的方式将模板创建为静态资源?
我试图创建一个静态资源,例如
<DataTemplate x:Key="ColumnTemplate">
<TextBox Text={I have no idea what to put here}/>
<OtherThings/>
<OtherThings/>
<DataTemplate>
并像使用它
<DataGrid>
<DataGrid.Columns>
<!-- Where does the Header go?, where does the binding go?-->
<!-- binds to column 2-->
<DataGridTemplateColumn CellTemplate="{StaticResource ColumnTemplate}">
<!-- binds to column 3-->
<DataGridTemplateColumn CellTemplate="{StaticResource ColumnTemplate}">
</DataGrid.Columns>
</DataGrid>
但我真的不知道如何正确地从项目绑定到模板内的文本框。
我怎样才能做到这一点?
【问题讨论】:
标签: wpf xaml data-binding datatemplate