如何在UWP中设置datagrid(Windows社区工具包)项目库?
数据杂志有2列。每个列具有不同的CellTemplate。
第1列单元模板:
<DataTemplate x:DataType="m:ManagePicklistDataGridRangeColumnCellModel">
<RelativePanel Height="14" Background="White" HorizontalAlignment="Stretch">
<TextBlock Name="HeaderTextBlock" Text="{x:Bind RangeText}" FontSize="12" Height="14" LineHeight="14" FontFamily="SegoeUI" Foreground="{ThemeResource DefaultTextColor}"/>
<FontIcon RelativePanel.RightOf="HeaderTextBlock" FontSize="12" Glyph="" FontFamily="Segoe MDL2 Assets"/>
</RelativePanel>
</DataTemplate>
第2列单元模板:
<DataTemplate x:DataType="m:ManagePicklistDataGridRangeColumnCellModel">
<RelativePanel Height="14" Background="White" HorizontalAlignment="Stretch">
<TextBlock Name="HeaderTextBlock" Text="{x:Bind CellText}" FontSize="12" Height="14" LineHeight="14" FontFamily="SegoeUI" Foreground="{ThemeResource DefaultTextColor}"/>
</RelativePanel>
</DataTemplate>
我不知道如何在数据杂志中创建和列,并使用C#中的ViewModel在Cell Tempalte中的TextBlock属性绑定了TextBlock属性。
The DataGrid have 2 Columns.Each Column have Different CellTemplate.
Column 1 Cell Template :
<DataTemplate x:DataType="m:ManagePicklistDataGridRangeColumnCellModel">
<RelativePanel Height="14" Background="White" HorizontalAlignment="Stretch">
<TextBlock Name="HeaderTextBlock" Text="{x:Bind RangeText}" FontSize="12" Height="14" LineHeight="14" FontFamily="SegoeUI" Foreground="{ThemeResource DefaultTextColor}"/>
<FontIcon RelativePanel.RightOf="HeaderTextBlock" FontSize="12" Glyph="" FontFamily="Segoe MDL2 Assets"/>
</RelativePanel>
</DataTemplate>
Column 2 Cell Template :
<DataTemplate x:DataType="m:ManagePicklistDataGridRangeColumnCellModel">
<RelativePanel Height="14" Background="White" HorizontalAlignment="Stretch">
<TextBlock Name="HeaderTextBlock" Text="{x:Bind CellText}" FontSize="12" Height="14" LineHeight="14" FontFamily="SegoeUI" Foreground="{ThemeResource DefaultTextColor}"/>
</RelativePanel>
</DataTemplate>
I don't know how to create and Columns in DataGrid and bind the TextBlock property in cell Tempalte using ViewModel in C#.How to set ItemSource of DataGrid using ObservableCollection in c#?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

DataGrid具有项目库属性,您可以将其绑定到收集中,也可以在后面的代码中设置值。
然后,每个
datagridtemplatecolumn
可以访问项目propety。例如,如果您的项目型号具有姓名和年龄属性。当前的CellTemplate目标类型是项目,您可以将DataGridTemplateColumn绑定到以下您可以使用标签获取每一列,然后调用
getCellContent
以检索单元格内容。它可以通过编辑内容的dataContext
来更新背后代码中的绑定。DataGrid has ItemSource property, you could bind it with your collection, and it could also be set value in the code behind.
Then each
DataGridTemplateColumn
could access Item propety. for example if your Item model has Name and Age property. And current CellTemplate target type is Item, you could set DataGridTemplateColumn binding like the followingYou could get each column with tag, then call
GetCellContent
to retrieve cell content. It could update the binding in the code behind by editing content'sDataContext