在运行时如何处理在WPF Toolkit DataGrid中显示的单元格数据? [英] How to I manipulate cell data being displayed in WPF Toolkit DataGrid at runtime?

查看:360
本文介绍了在运行时如何处理在WPF Toolkit DataGrid中显示的单元格数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下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 =Window2Height =300Width =500>
< StackPanel>
< tk:DataGrid x:Name =dataGrid
Margin =0 0 0 10
AutoGenerateColumns =False
CanUserAddRows =False
HeadersVisibility =Column
MaxHeight =400
IsReadOnly =True
背景=#fff
ColumnWidth =SizeToHeader>
< / tk:DataGrid>
< / StackPanel>
< / Window>

代码背后:

 使用系统; 
使用System.Collections.Generic;
使用System.Linq;
使用System.Windows;
使用System.Windows.Data;
使用System.Windows.Documents;
使用Microsoft.Windows.Controls;

命名空间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 =邮政编码;
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;组; }
public string LastName {get;组; }
public string Street {get;组; }
public string Location {get;组; }
public string ZipCode {get;组; }

public static List< Customer> GetCustomers()
{
列表< 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});
回报客户;
}

}
}


解决方案

请看下面的代码是否适合你。我已经使用一个DataGridTextColumn后代类覆盖了GenerateElement方法。在那里,您可以使用为给定单元格创建的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;
返回元素;
}
}

希望这有帮助,
问候p>

The following WPF code displays the contents of FirstName and ZipCode in the WPF Toolkit DataGrid.

However, I don't want to just display the data as it is but slightly modified, e.g. I might want to display all the zipcodes to display with a "-0000" on the end, or I may want to display "n/a" if a cell is blank.

I could only find CopyingCellClipboardContent which doesn't seem to do what I want.

I'm thinking I might need a Converter but am not sure how to go about it in this example.

How can I manipulate the cell content of the DataGrid cells at runtime?

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>

Code Behind:

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;
        }

    }
}

解决方案

Pls, see if code below would work fine for you. I've made an DataGridTextColumn descendant class with overriden GenerateElement method. There you could manipulate with the TextBlock control created for the given cell, e.g alter its value to add extra digits for zip codes.

...
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;
    }
}

hope this helps, regards

这篇关于在运行时如何处理在WPF Toolkit DataGrid中显示的单元格数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆