是否可以将两个不同的数据绑定到同一个DataGrid列。假设我拥有属性p1的A类,并且拥有属性p2的另一个B类。是否可以将p1和p2绑定到同一个datagrid列?DataGrid列的多个绑定
0
A
回答
1
事情是这样的:
<StackPanel>
<TextBlock Text="{Binding ClassAProperty}"/>
<TextBlock Text="{Binding ClassBProperty}"/>
</StackPanel>
将工作,只要类没有具有相同名称的任何属性。但是这是一种丑陋的黑客行为,并且很好运在这种方法将产生的所有虚假绑定错误中发现真正的绑定错误。
每种类型的属性到列的映射必须在某个地方居住,但它不必住在XAML中,这不是我放置它的地方。我会在我的视图模型中做到这一点。假设我还没有为我的ClassA和ClassB的对象视图模型类(和我不想要创建它们),我想实现这样的事情:
public class DataGridHelper
{
public Wrapper(object o)
{
ClassA a = o as ClassA;
if (a != null)
{
Column1 = a.Property1;
Column2 = a.Property2;
...
}
ClassB b = o as ClassA;
if (b != null)
{
Column1 = b.Property1;
Column2 = b.Property2;
...
}
public object Column1 { get; private set; }
public object Column2 { get; private set; }
}
...然后将DataGrid
的列绑定到DataGridHelper
对象的集合。
如果我没有有ClassAViewModel
和ClassBViewModel
班,我只希望实现两个Column1
,Column2
等特性。如果我需要支持双向绑定和验证,那就是要走的路。
2
您可以使用DataGridTemplate列包含两个不同的控件,每个控件绑定到不同的属性。
1
最简单的方法可能是MultiBinding。这里是关于如何使用MultiBinding的一个simple example(它利用了我喜欢的StringFormat属性)。
相关问题
- 1. 绑定多个列表DataGrid中
- 2. 的DataGrid的Silverlight列绑定
- 3. DataGrid视图中的多个绑定源
- 4. Silverlight的DataGrid和绑定列
- 5. 空的DataGrid绑定列表
- 6. WPF - 绑定两个列表中的DataGrid
- 7. DataGrid列自定义绑定
- 8. Silverlight datagrid重新绑定整个列
- 9. 绑定WPF DataGrid列到另一个
- 10. 绑定列表到DataGrid Silverlight
- 11. DataGrid - 绑定到MVVM列表的列表
- 12. 绑定DataGrid的列标题的DataContext
- 13. WPF DataGrid ClipboardBinding可能的多重绑定?
- 14. WPF DataGrid绑定
- 15. WPF Datagrid绑定自定义列标题
- 16. DataGrid对象的绑定列表
- 17. 绑定的对象到DataGrid列
- 18. WPF DataGrid绑定列之间的数据
- 19. 绑定WPF DataGrid文本列的前景
- 20. C#xaml datagrid绑定'值的列表'
- 21. 如何将两个列表绑定到Wpf DataGrid的两列?
- 22. Datagrid在代码背后绑定与XAML中的DataGrid绑定
- 23. WPF DataGrid多选选项绑定
- 24. DataGrid的列定义绑定到具有未知列的DataTable?
- 25. DataGrid双向绑定
- 26. 绑定XElement到Datagrid
- 27. Datagrid绑定问题
- 28. 在DataTemplate DataGrid绑定
- 29. 棱镜/ MVVM:绑定列到DataGrid中
- 30. 绑定子DataGrid父母列表功能
是的StackPanel是凌乱。创建一个DataGridHelper看起来很顺利。谢谢! – Reflux 2010-08-13 21:13:16