2011-12-05 43 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> 

這可能做的伎倆。 .... :)