WPF中的DataGridView列绑定 [英] DataGridView Column binding in WPF
问题描述
我要设计一个 DataGrid
,如下图所示:
I want to design a DataGrid
as shown in the picture below:
我打算绑定 DataGrid
到类的对象列表。我计划的班级是
I am planning to bind the DataGrid
to list of objects of a class. The class I was planning is
class Class1
{
public Int32 Index { get; set; }
public string Colour { get; set; }
public string Location { get; set; }
public string Srno { get; set; }
}
我有问题。我想再拥有一个color属性,可以将其直接绑定到column2中 DataGrid
的颜色。但是由于我打算将<code> DataGrid 绑定设置为该对象的列表,因此新属性将被识别为列本身。如何避免这种情况?任何建议。
I have a problem. I would like to have one more colour property which I can bind directly to the colour of the DataGrid
in column2. But since I am planning to set DataGrid
binding to list of this object, the new property will be recognized as a column itself. How do I avoid that? Any suggetions.
推荐答案
您可以设置自动生成列为假
并接受您有责任提供要设置的列列表,以避免设置DataSource或DataMember属性时自动生成列。
You can set AutoGenerateColumns to False
and take responsibility in your hand to provide the list of columns you want to avoid auto generation of columns when DataSource or DataMember properties are set.
<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding SourceCollection}">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Index}"/>
<DataGridTextColumn Binding="{Binding Colour}"/>
<DataGridTextColumn Binding="{Binding Location}"/>
<DataGridTextColumn Binding="{Binding Srno}"/>
</DataGrid.Columns>
</DataGrid>
这篇关于WPF中的DataGridView列绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!