如何动态地在$ C $加器C-背后动态

2023-09-03 06:41:21 作者:糖果味的爱恋

我通过IDictionary的绑定一个DataGrid动态数据:http://www.scottlogic.co.uk/blog/colin/2009/04/binding-a-silverlight-datagrid-to-dynamic-data-via-idictionary/comment-page-1/#comment-8681

但我不希望在XAML中定义的列(下面是它是如何在科林·埃伯哈特的帖子做了

 <数据:DataGrid.Columns>
    <数据:DataGridTextColumn标题=用的名字绑定={结合转换器= {的StaticResource RowIndexConverter},ConverterParameter =用的名字}/>
< /数据:DataGrid.Columns>
 

所以,我写了下面的code,试图做同样的事情在code落后,但code不调用RowIndexConverter。一定有什么缺失。

code:

  //添加列

DataGridTextColumn textColumn =新DataGridTextColumn();
textColumn.Header =用的名字;

绑定绑定=新的绑定(用的名字);
bind.Converter =新RowIndexConverter();
bind.ConverterParameter =用的名字;

textColumn.Binding =绑定;
_dataGrid.Columns.Add(textColumn);
 

的code座(这里的上下文):

  //生成一些假数据
随机兰特=新的随机();
的for(int i = 0; I< 200;我++)
{
    鳞次栉比=新行();
    行[用的名字] = s_names [rand.Next(s_names.Length)];
    行[姓] = s_surnames [rand.Next(s_surnames.Length)];
    行[年龄] = rand.Next(40)+ 10;
    行[Shoesize] = rand.Next(10)+ 5;

    _rows.Add(行);
}

//绑定到我们的DataGrid
_dataGrid.ItemsSource = _rows;

公共类行
{
    私人字典<字符串,对象> _data =新字典<字符串,对象>();

    公共对象本[字符串索引]
    {
        得到
        {
            返回_data [指数]
        }
        组
        {
            _data [指数] =值;
        }
    }
}
 
操作今日重点加这个板块,开始布局下半年

解决方案

该转换器在数据通过属性路径得到后调用。由于在行没有用的名字属性,这是行不通的(你可以看到在输出窗口绑定除外)。

我通过更改绑定定义来解决它

 绑定绑定=新的Binding();
    bind.Mode = BindingMode.OneWay;
 

因为你不能有双向不带路径(我有没有第二行的除外)的结合。 由于没有属性路径是有道理的,在第二个想法,因为我们希望绑定到全行的对象,而不是它的一个属性。

请注意:与测试VS 2008 SP1,WPF项目

I am Binding a Datagrid to dynamic data via IDictionary: http://www.scottlogic.co.uk/blog/colin/2009/04/binding-a-silverlight-datagrid-to-dynamic-data-via-idictionary/comment-page-1/#comment-8681

But I do not want to define any columns in the xaml (below is how it is done in Colin Eberhardt's post

<data:DataGrid.Columns>
    <data:DataGridTextColumn Header="Forename" Binding="{Binding Converter={StaticResource RowIndexConverter}, ConverterParameter=Forename}" />
</data:DataGrid.Columns>

So I have written the following code to try and do the same thing in the code behind, but the code does not call the RowIndexConverter. Something must be missing.

Code:

// add columns

DataGridTextColumn textColumn = new DataGridTextColumn();
textColumn.Header = "Forename";

Binding bind = new Binding("Forename");
bind.Converter = new RowIndexConverter() ;
bind.ConverterParameter = "Forename";            

textColumn.Binding = bind;
_dataGrid.Columns.Add(textColumn);

Rest of the code (here for context):

// generate some dummy data
Random rand = new Random();
for (int i = 0; i < 200; i++)
{
    Row row = new Row();
    row["Forename"] = s_names[rand.Next(s_names.Length)];
    row["Surname"] = s_surnames[rand.Next(s_surnames.Length)];
    row["Age"] = rand.Next(40) + 10;
    row["Shoesize"] = rand.Next(10) + 5;

    _rows.Add(row);
}

// bind to our DataGrid
_dataGrid.ItemsSource = _rows;    

public class Row
{
    private Dictionary<string, object> _data = new Dictionary<string, object>();

    public object this[string index]
    {
        get
        {
            return _data[index];
        }
        set
        {
            _data[index] = value;
        }
    }
}

解决方案

The converter is called after getting at the data through the property path. As there is no "Forename" property in Row, it does not work (you can see the Binding exception in the Output window).

I solved it by changing the Binding definition to:

    Binding bind = new Binding();
    bind.Mode = BindingMode.OneWay;

as you cannot have two-way binding without a path (the exception I got without the second line). Having no property path makes sense, on second thought, as we want to bind to the full Row object, not to one of its properties.

Note: tested with VS 2008 SP1, WPF project.