【问题标题】:Silverlight RadGrid Copy operation shows class name instead of bound valueSilverlight RadGrid 复制操作显示类名而不是绑定值
【发布时间】:2011-12-13 09:45:08
【问题描述】:

我正在尝试使用 Telerik 的 RadGridView(Silverlight 版本)实现复制机制,但我遇到了一个严重的问题。 我的 Datagrid 绑定到 ObservableCollection。这是 MyRow 类:

public class MyRow  
{ 
    public List<string> ColumnNames { get; set; } 
    public List<decimal?> Values { get; set; } 

    public String TimeStamp { get; set; } 

    public MyRow() 
    { 
        ColumnNames = new List<string>(); 
        Values = new List<decimal?>(); 
    } 
}

如您所见,列是动态创建的,因为MyRow 对象可以保存任意数量的值(因此必须创建列数)。为ColumnNames 中的每个String 生成列,并使用参数化的ValueConverter 绑定值。

private void createColumns(ObservableCollection<MyRow> values) 
{ 
    while (dataGrid.Columns.Count > 1) 
        dataGrid.Columns.RemoveAt(1); 

    dataGrid.Columns[0].Header = CommonStrings.TimeStamp; 
    int columnCount = values.First().ColumnNames.Count; 

    for (int i = 0; i < columnCount; i++) 
    { 
        GridViewDataColumn col = new GridViewDataColumn(); 
        col.Header = values.First().ColumnNames[i]; 
        col.DataType = typeof(MyRow); 
        Binding bnd = new Binding(); 
        bnd.Converter = new MyRowCollectionConverter(); 
        bnd.ConverterParameter = i; 
        col.DataMemberBinding = bnd; 
        dataGrid.Columns.Add(col); 
    } 
}

但是,当我尝试复制 (CTRL+C) 时,我得到的是:

TimeStamp               Value1 
12.12.2011. 9:51:59     MyProject.MyRow 
12.12.2011. 9:52:59     MyProject.MyRow 
12.12.2011. 9:53:59     MyProject.MyRow 
12.12.2011. 9:54:59     MyProject.MyRow

Value1 是包含在List&lt;String&gt; ColumnNames 中的名称。那部分没问题。但是,我没有得到数据(这是十进制值,例如 0.242524312),而是得到了类名。

如何使复制操作保留 MyRow 对象的绑定属性中的值而不是其类名(我猜它在内部的任何内容上调用 ToString() 方法)?

(问题也发布在Telerik's forum)。

【问题讨论】:

    标签: c# binding telerik copy radgridview


    【解决方案1】:

    问题是绑定不能那样工作。

    正确的解决方案是绑定MyRow属性Values,然后在ValueConverter中提取合适的值(通过参数索引)。

    这有效(与 Binding 更改的 for 循环相同):

    for (int i = 0; i < columnCount; i++) 
    { 
        /* ... */
    
        Binding bnd = new Binding("Values"); 
    
        /* ... */
    } 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-02
      • 1970-01-01
      • 2017-09-05
      • 2015-05-29
      • 2011-09-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多