Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 列标题为c的gridview视图模型#_C#_Mvvm_Datatable_Data Annotations_Observablecollection - Fatal编程技术网

C# 列标题为c的gridview视图模型#

C# 列标题为c的gridview视图模型#,c#,mvvm,datatable,data-annotations,observablecollection,C#,Mvvm,Datatable,Data Annotations,Observablecollection,我需要一个解决方案 我需要为栅格视图创建视图模型。此viewmodel应该是强类型的。前 List<Person> lstPersons=new List<Person>(); 像这样的。但我有两个问题 我不知道如何在运行时更改此显示名称 我正在使用telerik RADGridView。当我使用AutoGenerateColumns=“True”和ShowColumnFooters=“True”时,整个UI都会被阻塞。我认为这是telerik控制系统的错误。所以我必须

我需要一个解决方案

我需要为栅格视图创建视图模型。此viewmodel应该是强类型的。前

List<Person> lstPersons=new List<Person>();
像这样的。但我有两个问题

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

  • 我正在使用telerik RADGridView。当我使用
    AutoGenerateColumns=“True”
    ShowColumnFooters=“True”
    时,整个UI都会被阻塞。我认为这是telerik控制系统的错误。所以我必须在XAML中定义所有列,并为每个列添加绑定路径

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

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


    请随便问任何问题。我不确定上面的描述是否对每个人都清楚。

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

    我假设您要显示的列是固定的。。。。因此,您的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;
    
     }
    
    类MyViewModel
    {
    //在Xaml和INotifyPropertyChanged中为视图级别上需要的每个属性实现属性和适当的绑定
    可观察的集合人员;
    字符串列标题器1;
    串柱头2;
    串柱头3;
    }
    
    XAML

    
    

    这可能会奏效……:)

    您可以继续在模型上使用
    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.
        }
    }
    
    PublicPeopleGridViewModel
    {
    公众观察收集人员;
    公共字典PersonColumnHeaders;
    PublicPeopleGridViewModel()
    {
    //1.在此处写C#,将财产名称与(人)的类型区分开来
    //2.也获取该属性的DisplayName属性值。
    //3.将propertyName.ToString()和DisplayName字符串作为键/值对添加到字典中。
    //结果是,您有一个列标题集合,这些标题以对象上属性的默认值开头……但是它们可以在运行时进行操作,并且您没有像adcool的示例中那样100%地硬键入它们。
    }
    }
    
    我认为《秀专栏》的页脚交易是一个电视台的问题,正如你所建议的那样

            <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> 
    
    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.
        }
    }