【问题标题】:How to I manipulate cell data being displayed in WPF Toolkit DataGrid at runtime?如何在运行时操作 WPF Toolkit DataGrid 中显示的单元格数据?
【发布时间】:2009-12-02 16:25:01
【问题描述】:

以下 WPF 代码显示 WPF Toolkit DataGrid 中 FirstName 和 ZipCode 的内容。

但是,我不想只显示数据,而是稍作修改,例如我可能想显示所有邮政编码以在末尾显示“-0000”,或者如果单元格为空白,我可能想显示“n/a”。

我只能找到 CopyingCellClipboardContent 似乎没有做我想要的。

我想我可能需要一个转换器,但不知道在这个例子中如何去做。

如何在运行时操作 DataGrid 单元格的单元格内容?

XAML:

<Window x:Class="TestControl3423.Window2"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:tk="http://schemas.microsoft.com/wpf/2008/toolkit"
    Title="Window2" Height="300" Width="500">
    <StackPanel>
        <tk:DataGrid x:Name="dataGrid" 
                     Margin="0 0 0 10"
                     AutoGenerateColumns="False" 
                     CanUserAddRows="False"
                     HeadersVisibility="Column" 
                     MaxHeight="400"
                     IsReadOnly="True"
                     Background="#fff"
                     ColumnWidth="SizeToHeader">
        </tk:DataGrid>
    </StackPanel>
</Window>

代码隐藏:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Data;
using System.Windows.Documents;
using Microsoft.Windows.Controls;

namespace TestControl3423
{
    public partial class Window2 : Window
    {
        public Window2()
        {
            InitializeComponent();

            dataGrid.ItemsSource = Customer.GetCustomers();

            dataGrid.Columns.Clear();

            DataGridTextColumn dgtc1 = new DataGridTextColumn();
            dgtc1.Header = "First Name";
            dgtc1.Binding = new Binding("FirstName");
            dgtc1.Width = new DataGridLength(1, DataGridLengthUnitType.Auto);
            dataGrid.Columns.Add(dgtc1);

            DataGridTextColumn dgtc2 = new DataGridTextColumn();
            dgtc2.Header = "Zip Code";
            dgtc2.Binding = new Binding("ZipCode");
            dgtc2.Width = new DataGridLength(1, DataGridLengthUnitType.Auto);
            dgtc2.CopyingCellClipboardContent += new EventHandler<DataGridCellClipboardEventArgs>(dgtc2_CopyingCellClipboardContent);
            dataGrid.Columns.Add(dgtc2);
        }

        void dgtc2_CopyingCellClipboardContent(object sender, DataGridCellClipboardEventArgs e)
        {
            DataGridTextColumn dgtc = sender as DataGridTextColumn;
            dgtc.SetValue(dgtc.GetValue() + "-0000"); //ERROR
        }
    }

    public class Customer
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Street { get; set; }
        public string Location { get; set; }
        public string ZipCode { get; set; }

        public static List<Customer> GetCustomers()
        {
            List<Customer> customers = new List<Customer>();
            customers.Add(new Customer { FirstName = "Jim", LastName = "Jones", ZipCode = "23434" });
            customers.Add(new Customer { FirstName = "Joe", LastName = "Adams", ZipCode = "12312" });
            customers.Add(new Customer { FirstName = "Jake", LastName = "Johnson", ZipCode = "23111" });
            customers.Add(new Customer { FirstName = "Angie", LastName = "Reckar", ZipCode = "54343" });
            customers.Add(new Customer { FirstName = "Jean", LastName = "Anderson", ZipCode = "16623" });
            return customers;
        }

    }
}

【问题讨论】:

    标签: c# wpf datagrid wpftoolkit


    【解决方案1】:

    请看下面的代码是否适合您。我用重写的 GenerateElement 方法创建了一个 DataGridTextColumn 后代类。在那里,您可以使用为给定单元格创建的 TextBlock 控件进行操作,例如更改其值以添加额外的邮政编码数字。

    ...
    DataGridTextColumn dgtc2 = new ExtendedDataGridTextColumn(); 
    dgtc2.Header = "Zip Code"; 
    ...
    
    public class ExtendedDataGridTextColumn : DataGridTextColumn 
    {
        protected override FrameworkElement GenerateElement(DataGridCell cell, object dataItem)
        {
            TextBlock element = (TextBlock)base.GenerateElement(cell, dataItem);
            element.Text = ((Customer)dataItem).ZipCode + "-0000";
            return element;
        }
    }
    

    希望这会有所帮助, 问候

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-05-27
      • 2010-11-28
      • 2021-12-28
      • 1970-01-01
      • 2019-01-28
      • 2011-10-22
      • 2015-11-07
      相关资源
      最近更新 更多