2011-12-05 45 views
4

viewmodel我需要一个解决方案。gridview与列标题c#

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

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

这样的事情。此外,我应该能够拥有自定义列标题名称。我可以去与数据注解与启用AutoGenerateColumns="True" 一样,

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

这样的事情。但我有这个问题2。

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

  2. 使用telerik RADGridView的Im。当我使用AutoGenerateColumns="True"ShowColumnFooters="True"时,整个用户界面都会陷入困境。我认为这是telerik控件的错误。所以我必须在XAML中定义所有列,并为每个列添加绑定路径。

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

如何创建一个ViewModel来实现这个?有什么建议么 ?

随时问任何问题。我不知道上面的描述对每个人都是清楚的。

回答

1

您可以继续在y上使用DisplayName属性我们的模型,但正如您所指出的,在运行时无法更改它。要做到这一点,实现您的视图模型的字典,这些填充

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的问题,因为你的建议。

1

我认为你要OT显示器固定在列....这样你的视图模型会像

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> 

这可能做的伎俩。 .... :)