ViewModel 不显示数据网格中的元素
如何停止 WPF DataGrid 表单自动显示绑定到数据网格的元素。
我有这个视图模型,我不希望它显示元素“历史记录”。 我尝试过 [Display(AutoGenerateField = false)] 但它不起作用。
我在这里的方式将显示潜在客户、呼叫、最后呼叫,然后再次显示所有元素。
<DataGrid ItemsSource="{Binding Leads}" Margin="0" FontSize="20">
<DataGrid.Columns>
<DataGridTextColumn Header="Lead" Binding="{Binding Lead}" Width="150" />
<DataGridTextColumn Header="Calls" Binding="{Binding Calls}" Width="150" />
<DataGridTextColumn Header="LastCall" Binding="{Binding LastCall}" Width="*" />
</DataGrid.Columns>
查看模型
public class LeadViewModel : ViewModel
{
[Display(AutoGenerateField = false)]
public int Lead { get; set; }
[Display(AutoGenerateField = false)]
public int Calls
{
get
{
return History.Count();
}
}
[Display(AutoGenerateField = false)]
public int EndCalls
{
get
{
return (from h in History
where h.DailTime.End != null
select h).Count();
}
}
[Display(AutoGenerateField = false)]
public int Jobs
{
get
{
return (from h in History
where h.DailTime.End != null
select h.ProjectJob).Distinct().Count();
}
}
[Display(AutoGenerateField = false)]
public DateTime? LastCall
{
get
{
return (from h in History
orderby h.DailTime.Start descending
select h.DailTime.Start).FirstOrDefault();
}
}
[Display(AutoGenerateField = false)]
public IQueryable<DailHistory> History { get; set; }
}
How can i stop a WPF DataGrid form automatic display an element binded to an datagrid.
I have this view model where i don't want it to display the element "History".
I have tryed with [Display(AutoGenerateField = false)] but it dose not work.
The way i have it here will it display Lead, Calls, LastCall and then all the elements again.
<DataGrid ItemsSource="{Binding Leads}" Margin="0" FontSize="20">
<DataGrid.Columns>
<DataGridTextColumn Header="Lead" Binding="{Binding Lead}" Width="150" />
<DataGridTextColumn Header="Calls" Binding="{Binding Calls}" Width="150" />
<DataGridTextColumn Header="LastCall" Binding="{Binding LastCall}" Width="*" />
</DataGrid.Columns>
View Model
public class LeadViewModel : ViewModel
{
[Display(AutoGenerateField = false)]
public int Lead { get; set; }
[Display(AutoGenerateField = false)]
public int Calls
{
get
{
return History.Count();
}
}
[Display(AutoGenerateField = false)]
public int EndCalls
{
get
{
return (from h in History
where h.DailTime.End != null
select h).Count();
}
}
[Display(AutoGenerateField = false)]
public int Jobs
{
get
{
return (from h in History
where h.DailTime.End != null
select h.ProjectJob).Distinct().Count();
}
}
[Display(AutoGenerateField = false)]
public DateTime? LastCall
{
get
{
return (from h in History
orderby h.DailTime.Start descending
select h.DailTime.Start).FirstOrDefault();
}
}
[Display(AutoGenerateField = false)]
public IQueryable<DailHistory> History { get; set; }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Bij 将 AutoGenerateColumns 属性设置为 false:
默认为 true,从而将 itemssource 的所有列添加到数据网格中。
Bij setting the AutoGenerateColumns property to false:
It defaults to true, and thus adding all the columns of the itemssource to the datagrid.