ViewModel 不显示数据网格中的元素

发布于 2024-10-05 10:28:40 字数 1765 浏览 1 评论 0原文

如何停止 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笔落惊风雨 2024-10-12 10:28:40

Bij 将 AutoGenerateColumns 属性设置为 false:

<DataGrid AutoGenerateColumns="False">

</DataGrid>

默认为 true,从而将 itemssource 的所有列添加到数据网格中。

Bij setting the AutoGenerateColumns property to false:

<DataGrid AutoGenerateColumns="False">

</DataGrid>

It defaults to true, and thus adding all the columns of the itemssource to the datagrid.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文