我正在尝试使用从System.Data.DataRow派生的类型进行数据绑定,该类型具有返回计算数据的公共属性,该计算数据不是我的DataTable中的列。
我可以成功地将我的GridViewDataColumn数据绑定到我的数据表中存在的属性(TestString),但我无法数据绑定到我的属性(TestHashID),该属性返回不在数据表中的数据。
包含的样本:
XAML
<Window x:Class="Junk.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
Title="MainWindow" Height="350" Width="525">
<Grid>
<telerik:RadGridView x:Name="cGrid" HorizontalAlignment="Left" VerticalAlignment="Top" Height="261" Width="445" Margin="39,25,0,0" />
</Grid>
Code Behind
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Telerik.Windows.Controls;
namespace Junk
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
TestTable mTestTable = new TestTable();
public MainWindow()
{
InitializeComponent();
CreateData();
CreateGrid();
}
void CreateData()
{
Test sRow;
sRow = (Test)mTestTable.NewRow();
sRow.TestString = "123";
mTestTable.Rows.Add(sRow);
sRow = (Test)mTestTable.NewRow();
sRow.TestString = "456";
mTestTable.Rows.Add(sRow);
sRow = (Test)mTestTable.NewRow();
sRow.TestString = "789";
mTestTable.Rows.Add(sRow);
}
void CreateGrid()
{
cGrid.AutoGenerateColumns = false;
var sCol1 = new GridViewDataColumn();
sCol1.Header = "Test_String";
sCol1.DataMemberBinding = new Binding("TestString");
cGrid.Columns.Add(sCol1);
var sCol2 = new GridViewDataColumn();
sCol2.Header = "Test_HashID";
sCol2.DataMemberBinding = new Binding("TestHashID");
cGrid.Columns.Add(sCol2);
cGrid.ItemsSource = mTestTable;
}
}
///////////////////////////////////////////////////////////////////
// class TestTable
///////////////////////////////////////////////////////////////////
[Serializable]
public class TestTable : DataTable
{
public DataColumn TestStringColumn { get { return this.Columns["TestString"]; } }
public TestTable() : base("Test")
{
DataColumn sDataColumn = new DataColumn("TestString", typeof(string), "", System.Data.MappingType.Element);
sDataColumn.AllowDBNull = false;
sDataColumn.DefaultValue = "";
this.Columns.Add(sDataColumn);
}
protected override DataRow NewRowFromBuilder(DataRowBuilder builder) { return new Test(builder); }
protected override System.Type GetRowType() { return typeof(Test); }
} // End TestTable
//////////////////////////////////////////////////////////////////////
// class Test
//////////////////////////////////////////////////////////////////////
public partial class Test : DataRow
{
public TestTable TestTable { get { return (TestTable)this.Table; } }
public Test(DataRowBuilder rb)
: base(rb)
{
}
public string TestString
{
get
{
return this[TestTable.TestStringColumn].ToString().TrimEnd();
}
set
{
if (TestString != value.TrimEnd()) this[TestTable.TestStringColumn] = value.TrimEnd();
}
}
public int TestHashID
{
get { return TestString.GetHashCode(); }
set { ;}
}
}
}
这是结果(我无法发布截图):
当我运行此示例时,Test_String列将按预期填充数据,但Test_HashID列为空。
非常感谢任何帮助!