带有列标题的 gridview 的 viewmodel c#

发布于 2024-12-19 15:43:35 字数 756 浏览 0 评论 0原文

我需要一个解决方案。

我需要为网格视图创建一个视图模型。该视图模型应该是强类型的。前任。

List<Person> lstPersons=new List<Person>();

像这样的东西。另外,这样我应该能够拥有自定义列标题名称。我可以通过启用 AutoGenerateColumns="True" 来进行数据注释 像

class Person
{
      [DisplayName("Person Name")]
      public string name { get; set; }
}

这样的东西。但我对此有两个问题。

  1. 我不知道如何在运行时更改此显示名称。

  2. 我正在使用 telerik RADGridView。当我使用 AutoGenerateColumns="True"ShowColumnFooters="True" 时,整个 UI 都会卡住。我认为这是 Telerik 控件的错误。因此,我必须在 XAML 中定义所有列,并为每个列添加绑定路径。

无论如何,我认为这对于 DataTable 是可能的。但我觉得数据表是非常古老的结构和沉重的对象。

如何创建 Viewmodel 来实现这一目标?有什么建议吗?

如有任何问题,请随时提出。我不确定上面的描述是否每个人都清楚。

I need a solution for this.

I need to create a view model for grid view. This viewmodel should be a strong typed one. ex.

List<Person> lstPersons=new List<Person>();

something like this. also, with this I should be able to have custom column header names. I can go with data annotation with enabling AutoGenerateColumns="True"
like,

class Person
{
      [DisplayName("Person Name")]
      public string name { get; set; }
}

something like this. But I have 2 issues with this.

  1. I donno how to change this display name at run time.

  2. Im using telerik RADGridView. with that when I'm using AutoGenerateColumns="True" and ShowColumnFooters="True", the whole UI get stucked. I think this s an error with telerik controls. So I have to define all columns in XAML and add binding path for each as well.

Anyway, This is possible with DataTable I think. but I feel data tables are very oldie struct and heavy object.

How to create a Viewmodel to achieve this? Any suggestions ?

Feel free to ask any question. im not sure above description is clear to everyone.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

你怎么这么可爱啊 2024-12-26 15:43:35

您可以继续在模型上使用 DisplayName 属性,但正如您所指出的,无法在运行时更改它。为此,请在 ViewModel 中实现一个字典,该字典填充

public PeopleGridViewModel
{
    public ObservableCollection<Person> People;
    public Dictionary<string, string> PersonColumnHeaders;

    public PeopleGridViewModel()
    {
        // 1. write C# here to snag the property names from the typeof(Person)
        // 2. get the DisplayName attribute value for that property too.
        // 3. add the propertyName.ToString() and the DisplayName string to the dictionary as a key/value pair.

        // the result is you have a collection of column headers that start with the defaults from the propertys on the object... but they can be manipulated at run-time, and you don't havem them 100% hard typed like in adcool's example.
    }
}

我认为显示列页脚交易是 Telerik 问题,正如您所建议的那样。

You can continue to use the DisplayName attribute on your model, but as you pointed out, cannot change it at run-time. To do so, implement a dictionary in your ViewModel that populates

public PeopleGridViewModel
{
    public ObservableCollection<Person> People;
    public Dictionary<string, string> PersonColumnHeaders;

    public PeopleGridViewModel()
    {
        // 1. write C# here to snag the property names from the typeof(Person)
        // 2. get the DisplayName attribute value for that property too.
        // 3. add the propertyName.ToString() and the DisplayName string to the dictionary as a key/value pair.

        // the result is you have a collection of column headers that start with the defaults from the propertys on the object... but they can be manipulated at run-time, and you don't havem them 100% hard typed like in adcool's example.
    }
}

I think the show column footers deal is a Telerik issue as you suggested.

自由范儿 2024-12-26 15:43:35

试试这个关于数据绑定列标题的讨论

Try this discussion about data binding the column header.

走走停停 2024-12-26 15:43:35

我假设您想要显示的列是固定的......所以您的 ViewModel 将类似于

 class MyViewModel
 {
      //Implement Properties nad proper binding in Xaml and INotifyPropertyChanged for every property that you need on View Level
      ObservableCollection<Persons> persons;
      string columnheader1;
      string columnheader2;
      string columnheader3;

 }

XAML

        <telerik:RadGridView.Columns> 

            <telerik:GridViewDataColumn   DataMemberBinding="{Binding UserName}" Width="200">

                <telerik:GridViewDataColumn.Header> 

                    <TextBlock Text="{Binding Columnheader1}"></TextBlock> 

                </telerik:GridViewDataColumn.Header> 

                </telerik:GridViewDataColumn> 

            <telerik:GridViewDataColumn Header="{Binding Columnheader2}" DataMemberBinding="{Binding IsOnline}" MinWidth="200" Width="*"/> 

            <telerik:GridViewDataColumn Header="{Binding Columnheader3}" DataMemberBinding="{Binding LastActivityDate}" MinWidth="200"/> 

        </telerik:RadGridView.Columns> 

这可能会成功......:)

I assume that the columns you want ot display is Fixed.... so your ViewModel Will be like

 class MyViewModel
 {
      //Implement Properties nad proper binding in Xaml and INotifyPropertyChanged for every property that you need on View Level
      ObservableCollection<Persons> persons;
      string columnheader1;
      string columnheader2;
      string columnheader3;

 }

XAML

        <telerik:RadGridView.Columns> 

            <telerik:GridViewDataColumn   DataMemberBinding="{Binding UserName}" Width="200">

                <telerik:GridViewDataColumn.Header> 

                    <TextBlock Text="{Binding Columnheader1}"></TextBlock> 

                </telerik:GridViewDataColumn.Header> 

                </telerik:GridViewDataColumn> 

            <telerik:GridViewDataColumn Header="{Binding Columnheader2}" DataMemberBinding="{Binding IsOnline}" MinWidth="200" Width="*"/> 

            <telerik:GridViewDataColumn Header="{Binding Columnheader3}" DataMemberBinding="{Binding LastActivityDate}" MinWidth="200"/> 

        </telerik:RadGridView.Columns> 

This might do the trick..... :)

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