自定义 DataGrid 控件中自动生成的列

发布于 2024-12-29 01:52:53 字数 1968 浏览 2 评论 0原文

阅读有关如何自定义自动的优秀文章后,生成的列,我遇到了一个问题。

在尝试在 DataGrid 控件中自定义自动生成的列时,我想做一些简单的事情,例如确保所有数字列值都右对齐。为此,我创建了一个 DataTemplate,如下所示:

<DataGrid x:Name="MyGrid" AutoGeneratingColumn="MyGrid_AutoGeneratingColumn">
  <DataGrid.Resources>
    <DataTemplate x:Key="IntegerTemplate">
      <TextBlock Text="{Binding}" HorizontalAlignment="Right"/>
    </DataTemplate>
  </DataGrid.Resources>
</DataGrid>

然后,在 AutoGenerateColumn DataGrid 事件处理程序中,我想分配此通用 DataTemplate 作为所有整数(即数字)列的 CellTemplate

public void MyWindow_AdjustColumnTemplateBasedOnType(
              DataGridAutoGeneratingColumnEventArgs e)
{
  if (/*This is a column I want to change*/)
  {
    DataGridTemplateColumn column=new DataGridTemplateColumn(); 

    column.Header=e.PropertyName;
    column.CellTemplate=MyGrid.FindResource("IntegerTemplate") as DataTemplate; 
    e.Column=column; 
  }
}

问题是 TextBlockText 列的值code> 没有显示出想要的结果。我看到的是:

在此处输入图像描述

通过将属性 Text 设置为 “{Binding}”空绑定语法 > 显然是错误的。设置基于路径的绑定确实会产生所需的结果。也就是说,如果我使用以下内容设置(硬编码数据路径)绑定:

  <DataGrid.Resources>
    <DataTemplate x:Key="IntegerTemplate">
      <!-- Binding hard set to ProductId -->
      <TextBlock Text="{Binding ProductId}" HorizontalAlignment="Right"/>
    </DataTemplate>
  </DataGrid.Resources>

那么一切都很好,但我的通用 DataTemplate 不再通用。它只能用于 ProductId 列,而不是可重用于所有整数列,因为绑定固定为该特定数据项的值:

在此处输入图像描述

我应该使用什么正确的绑定,以便通用 DataTemplate 实际上使用相应 中的任何值列的 ItemSource 属性它是相关联的。

After reading the excellent article on how to Customize Auto-Generated Columns, I have come across an issue.

While trying to customize auto-generated columns in a DataGrid control, I want to do simple things like make sure that all numeric column values are right-aligned. To this end, I created a DataTemplate as follows:

<DataGrid x:Name="MyGrid" AutoGeneratingColumn="MyGrid_AutoGeneratingColumn">
  <DataGrid.Resources>
    <DataTemplate x:Key="IntegerTemplate">
      <TextBlock Text="{Binding}" HorizontalAlignment="Right"/>
    </DataTemplate>
  </DataGrid.Resources>
</DataGrid>

Then, in the AutoGeneratingColumn DataGrid event handler, I want to assign this generic DataTemplate as the CellTemplate for all integral (i.e., numeric) columns:

public void MyWindow_AdjustColumnTemplateBasedOnType(
              DataGridAutoGeneratingColumnEventArgs e)
{
  if (/*This is a column I want to change*/)
  {
    DataGridTemplateColumn column=new DataGridTemplateColumn(); 

    column.Header=e.PropertyName;
    column.CellTemplate=MyGrid.FindResource("IntegerTemplate") as DataTemplate; 
    e.Column=column; 
  }
}

The problem is that the value of the Text column of the TextBlock does not display the desired results. Instead of seeing the right justified value in each cell whose column has this DataTemplate as its CellTemplate, I see:

enter image description here

Using the empty binding syntax by setting the attribute Text to "{Binding}" is obviously incorrect. Setting a path based binding does produce the desired result. That is, if I set a (hard-coded data path) binding using something like:

  <DataGrid.Resources>
    <DataTemplate x:Key="IntegerTemplate">
      <!-- Binding hard set to ProductId -->
      <TextBlock Text="{Binding ProductId}" HorizontalAlignment="Right"/>
    </DataTemplate>
  </DataGrid.Resources>

Then all is well, but my generic DataTemplate is no longer generic. Instead of it being reusable for all integer columns, it can only be used for the ProductId column, because the binding is fixed to the value of that specific data item:

enter image description here

What is the correct binding I should use so that the generic DataTemplate actually uses whatever value is in the corresponding ItemSource property for the column with which it is associated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

左耳近心 2025-01-05 01:52:53

我相信样式会在这里解决您的问题。

        private void MyGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
        {  
            if (/*This is a column I want to change*/)
            {
                DataGridColumn column = e.Column;
                column.CellStyle = MyGrid.FindResource("IntegerTemplate") as Style;

            }
        }

在 XAML 中你可以这样写

<Style TargetType="DataGridCell" x:Key="IntegerTemplate">
     <Setter Property="FontWeight" Value="Bold"></Setter>
</Style>    

I believe styles will solve your issue here .

        private void MyGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
        {  
            if (/*This is a column I want to change*/)
            {
                DataGridColumn column = e.Column;
                column.CellStyle = MyGrid.FindResource("IntegerTemplate") as Style;

            }
        }

and in XAML you can write

<Style TargetType="DataGridCell" x:Key="IntegerTemplate">
     <Setter Property="FontWeight" Value="Bold"></Setter>
</Style>    
灵芸 2025-01-05 01:52:53

在 DataGrid 上下文中使用 TemplateColumn 时,通常还必须使用 ValueConverter。

此 ValueConverter 将 ViewModels 对象中的数据(这实际上是您想要使用 TemplateColum 而不是普通 TextColumn 的原因)转换为可表示的字符串形式。

您是否有这样的转换器,或者您是否在 ViewModels 对象中提供 ToString() 方法?如果不尝试一下,也许会有帮助......

When using a TemplateColumn in a DataGrid context you usually have to use a ValueConverter as well.

This ValueConverter converts the data from you ViewModels object (which is in fact the reason why you want to use a TemplateColum instead of an ordinary TextColumn) to an representable string form.

Do you have such a converter or do you provide a ToString() method in your ViewModels object ? If not try it, maybe that helps ...

土豪我们做朋友吧 2025-01-05 01:52:53

对于那些苦于“我应该使用什么正确的绑定,以便通用 DataTemplate 实际上使用与其关联的列的相应 ItemSource 属性中的任何值”的人。

请参阅 Jay_Wang 在此链接中的回答:

http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/8b2e94b7-3c44-4642-8acc-851de5285062

重要的一点是覆盖 System.Windows .FrameworkElement 提供了GenerateElement 函数,使其能够以编程方式设置绑定以使用列名。

希望这有帮助。

For people struggling with the "What is the correct binding I should use so that the generic DataTemplate actually uses whatever value is in the corresponding ItemSource property for the column with which it is associated."

See Jay_Wang's answer in this link:

http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/8b2e94b7-3c44-4642-8acc-851de5285062

The important bit is overriding the System.Windows.FrameworkElement GenerateElement function so that it can programatically set up the binding to use the column name.

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文