C# 数据网格列 XAML
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12230866/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DataGrid Column XAML
提问by alostr
I am using a DataGrid to show items from a database table, and I am using EF CodeFirst so the database query automatically generates an object.
我正在使用 DataGrid 来显示数据库表中的项目,并且我正在使用 EF CodeFirst,因此数据库查询会自动生成一个对象。
Here is my XAML:
这是我的 XAML:
<DataGrid Name="details" Margin="0,20,0,0" ItemsSource="{Binding}">
</DataGrid>
And this is the code behind it:
这是它背后的代码:
data = new DbLayer();
int cardNumId = (from dataCardNum in data.creditCards
where dataCardNum.creditCardNumber == cardNum
select dataCardNum.Id).First();
debits =new ObservableCollection<Debit>(( from billings in data.charges
where billings.creditCardNumber.Id == cardNumId
select billings).ToList());
DataContext = debits;
That resolves in filling my DataGrid with all the information from my database. The only problem is that I have two columns that I don't want to show. I tried to create a dataTemplate that will generate the grid with the columns I want, but when I bind it to the datacontext it showed no information.
这解决了用我的数据库中的所有信息填充我的 DataGrid 的问题。唯一的问题是我有两列不想显示。我试图创建一个 dataTemplate,它将生成带有我想要的列的网格,但是当我将它绑定到 datacontext 时,它没有显示任何信息。
Here is my dataTemplate:
这是我的数据模板:
<DataTemplate x:Key="debitShow" DataType="DataTemplate:MonthBill.Debit">
<DataGrid>
<DataGrid.Columns>
<DataGridTextColumn Header="amount" Binding="{Binding amount}"/>
<DataGridTextColumn Header="charge date" Binding="{Binding chargeDate}"/>
<DataGridCheckBoxColumn Header="charged" Binding="{Binding charged}"/>
<DataGridTextColumn Header="store name" Binding="{Binding storeName}"/>
<DataGridTextColumn Header="purchase date" Binding="{Binding debitDate}"/>
<DataGridTextColumn Header="description" Binding="{Binding description}"/>
</DataGrid.Columns>
</DataGrid>
</DataTemplate>
window xaml:
窗口xaml:
Debit class(the key attribute is for the codefirst database creation):
借记类(关键属性用于codefirst数据库创建):
class Debit
{
[Key]
public int Id { get; set; }
public int amount { get; set; }
public string storeName { get; set; }
public DateTime debitDate { get; set; }
public DateTime chargeDate { get; set; }
public string description { get; set; }
public creditCard creditCardNumber { get; set; }
public bool charged { get; set; }
}
Any ideas?
有任何想法吗?
采纳答案by Jakub Kaleta
If your objective is to display your data without the two columns that you don't need, I would suggest taking the simpler approach of just specifying the columns of your grid explicitly:
如果您的目标是在没有您不需要的两列的情况下显示数据,我建议采用更简单的方法,即明确指定网格的列:
<DataGrid ItemsSource="{Binding}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="amount" Binding="{Binding amount}"/>
<DataGridTextColumn Header="charge date" Binding="{Binding chargeDate}"/>
<DataGridCheckBoxColumn Header="charged" Binding="{Binding charged}"/>
<DataGridTextColumn Header="store name" Binding="{Binding storeName}"/>
<DataGridTextColumn Header="purchase date" Binding="{Binding debitDate}"/>
<DataGridTextColumn Header="description" Binding="{Binding description}"/>
</DataGrid.Columns>
</DataGrid>
Notice the AutoGenerateColumns="False"attribute.
注意AutoGenerateColumns="False"属性。
I would only use a data template if I wanted to control the way the cells are rendered. If you are happy with the default presentation I think you don't need a template.
如果我想控制单元格的呈现方式,我只会使用数据模板。如果您对默认演示感到满意,我认为您不需要模板。

