如何在UWP中设置datagrid(Windows社区工具包)项目库?

发布于 01-22 10:23 字数 1159 浏览 7 评论 0原文

数据杂志有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="&#xE700;" 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绮烟2025-01-29 10:23:21

我不知道如何绑定和设置datagrid的项目库?

DataGrid具有项目库属性,您可以将其绑定到收集中,也可以在后面的代码中设置值。

然后,每个datagridtemplatecolumn可以访问项目propety。例如,如果您的项目型号具有姓名和年龄属性。当前的CellTemplate目标类型是项目,您可以将DataGridTemplateColumn绑定到以下

<controls:DataGridTemplateColumn Header="Name" Tag="name">
    <controls:DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding Name}" />
            </StackPanel>
        </DataTemplate>
    </controls:DataGridTemplateColumn.CellTemplate>
</controls:DataGridTemplateColumn>

如何在C#?

中的代码中绑定文本文本

您可以使用标签获取每一列,然后调用getCellContent以检索单元格内容。它可以通过编辑内容的dataContext来更新背后代码中的绑定。

 <TextBlock Text="{Binding }" />

 var dataGridTemplateColumn = datagrid.Columns.FirstOrDefault(x => x.Tag?.Equals("name") == true) as DataGridTemplateColumn;    
 var stackpanel = dataGridTemplateColumn.GetCellContent(MyClasses[0]);
 stackpanel.DataContext = "Hello";

I don't know how to bind and set ItemSource of DataGrid?

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 following

<controls:DataGridTemplateColumn Header="Name" Tag="name">
    <controls:DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding Name}" />
            </StackPanel>
        </DataTemplate>
    </controls:DataGridTemplateColumn.CellTemplate>
</controls:DataGridTemplateColumn>

How to bind TextBlock text in code Behind in c#?

You 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's DataContext

 <TextBlock Text="{Binding }" />

 var dataGridTemplateColumn = datagrid.Columns.FirstOrDefault(x => x.Tag?.Equals("name") == true) as DataGridTemplateColumn;    
 var stackpanel = dataGridTemplateColumn.GetCellContent(MyClasses[0]);
 stackpanel.DataContext = "Hello";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文