WPF:如何获取 Binding.Converter
我使用 Binding 创建 DataGrid 列(其中 i 是 Int 值):
dataGrid.Columns.Add(new DataGridTextColumn
{
Header = i.ToString(),
Binding = CreateBinding(i),
});
private Binding CreateBinding(int num)
{
Binding bind = new Binding(string.Format("[{0}]", num));
bind.Converter = new CellValueConverter();
return bind;
}
在 CreateBinding 方法中,我可以访问 bind.Converter 属性。
我需要在某些处理程序中调用 Converter.Convert() 方法,但当我尝试访问它时没有 Converter 属性:
(dataGrid.Columns[clm] as DataGridTextColumn).Binding."no Converter property!"
如何获取为特定列创建的 CellValueConverter?
I create DataGrid Columns with Binding (where i is a Int value):
dataGrid.Columns.Add(new DataGridTextColumn
{
Header = i.ToString(),
Binding = CreateBinding(i),
});
private Binding CreateBinding(int num)
{
Binding bind = new Binding(string.Format("[{0}]", num));
bind.Converter = new CellValueConverter();
return bind;
}
In the CreateBinding method I have an access to bind.Converter property.
I need to call Converter.Convert() method in some handler, but there is no Converter property when I try to access it:
(dataGrid.Columns[clm] as DataGridTextColumn).Binding."no Converter property!"
How can I get my CellValueConverter which was created for particular Column?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为 绑定
DataGridBoundColumn
(和DataGridTextColumn
)上的 属性实际上返回一个BindingBase
实例,而不是Binding
。BindingBase
不支持转换器。您应该能够尝试投射:
This is because the Binding property on
DataGridBoundColumn
(andDataGridTextColumn
) actually returns aBindingBase
instance, not aBinding
.BindingBase
doesn't support converters.You should be able to try to cast: