문제

I'm trying to implement the Copy mechanism with Telerik's RadGridView (Silverlight version), but I have a serious problem. My Datagrid is bound to a ObservableCollection. Here is the MyRow class:

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?>(); 
    } 
}

As you can see, the columns are created dynamically, because the MyRow object can hold an arbitrary number of values (so that number of columns would have to be made). The columns are generated for each String in ColumnNames and the values are bound by using the parameterized 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); 
    } 
}

However, when I try to Copy (CTRL+C), this is what I get:

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 is the name contained in the List<String> ColumnNames. That part is ok. However, I don't get the data (which are the decimal values, like 0.242524312), but instead I get the class name.

What can I do to make the copy operation retain the values in the bound property of the MyRow object instead of its class name (I guess it calls the ToString() method on whatever is inside)?

(Question also posted on Telerik's forum).

도움이 되었습니까?

해결책

The problem is that binding doesn't work that way.

The right solution is to bind to the MyRow property Values, and then extract the appropriate value in the ValueConverter (by parameter index).

This works (the same for loop with Binding changed):

for (int i = 0; i < columnCount; i++) 
{ 
    /* ... */

    Binding bnd = new Binding("Values"); 

    /* ... */
} 
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top