如何在代码隐藏中动态添加转换器
我通过 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 中定义任何列(下面是 Colin Eberhardt 的帖子中的做法,
<data:DataGrid.Columns>
<data:DataGridTextColumn Header="Forename" Binding="{Binding Converter={StaticResource RowIndexConverter}, ConverterParameter=Forename}" />
</data:DataGrid.Columns>
所以我有编写以下代码来尝试在后面的代码中执行相同的操作,但该代码不会调用 RowIndexConverter 。
代码:
// 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);
代码的其余部分(此处用于上下文):
// 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;
}
}
}
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;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通过属性路径获取数据后调用转换器。由于 Row 中没有“Forename”属性,因此它不起作用(您可以在“输出”窗口中看到“绑定”异常)。
我通过将绑定定义更改为:
因为没有路径就无法进行双向绑定(我在没有第二行的情况下得到的例外)解决了这个问题。
再想一想,没有属性路径是有意义的,因为我们想要绑定到完整的 Row 对象,而不是它的属性之一。
注:使用 VS 2008 SP1、WPF 项目进行测试。
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:
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.