2010-01-08 66 views
0

問題:我試圖顯示的數據本質上是一些對象集合的集合。所以這些行可以是任何數字,對於數據網格來說都是正常的,列也可以是任何數字。對於數據網格來說這不是很正常。通常你有一定數量的列,你的行有所不同。 datagrid單元格可以是一個字符串,也可以是一個可通過組合框進行更改的值。Silverlight DataGrid - 綁定到對象集合的集合

試圖解決方案:我試圖動態地向數據網格添加列,雖然這工作得很好(在代碼隱藏中添加它們),但我遇到的真正問題是如何綁定到底層對象。數據正在動態構建,我嘗試了幾種格式。我嘗試了一個數組的集合,還有一個ObservableCollection的ObservableCollection。我可以綁定到對象,但由於Silverlight中的綁定必須綁定到屬性,所以我無法用這種方法來提供數據解決方案。

因此,我的解決方案是以更傳統的方式顯示數據,包括列表和數據網格。當您選擇列表中的項目時,它會重新填充數據網格中的數據以顯示對象。

問題:有沒有辦法將datagrid單元綁定到對象集合的集合?

我發現這個問題(WPF)看起來很相似,它沒有任何幫助。我認爲這是同一個問題。 WPF DataGrid: DataGridComboxBox ItemsSource Binding to a Collection of Collections

回答

0

我想我明白你在努力完成什麼,而且我確實有一個更優雅的解決方案來解決你的問題,它不涉及編寫任何自定義類。我在這個問題上寫了一篇博文。該博客面向Silverlight Toolkit中的DataGrid,但您可以輕鬆修改它以使用任何網格。

The solution is here.

讓我知道,如果這是你要找的人。

+0

我已經閱讀過,您的解決方案看起來確實很優雅。我沒有意識到你可以綁定到一個集合(就像你在模板字符串中做的那樣)。即Text ='{{Binding Periods [{0}]。{1}}}' 我認爲這僅限於WPF,Silverlight無法做到這一點。我會進一步調查。謝謝! – 2010-03-10 09:06:08

1

一種可能的解決方案是,而不是使用簡單的集合作爲內部對象,創建從集合派生的類和執行它ICustomTypeDescriptor。在接口實現中,遍歷集合的元素並相應地填充屬性描述符集合。一旦你這樣做,你應該能夠綁定到來自XAML的這些屬性。

實施例 - 基於字典,它可以綁定針對其鍵名(I壓縮到單一線的所有瑣碎方法的實施方式)中的數據對象:

class DictionaryDataObject : Dictionary<string, object>, ICustomTypeDescriptor 
{ 
    #region ICustomTypeDescriptor Members 

    public AttributeCollection GetAttributes() { return AttributeCollection.Empty; } 
    public string GetClassName() { return "DictionaryDataObject"; } 
    public string GetComponentName() { return null; } 
    public TypeConverter GetConverter() { return null; } 
    public EventDescriptor GetDefaultEvent() { return null; } 
    public PropertyDescriptor GetDefaultProperty() { return null; } 
    public object GetEditor(Type editorBaseType) { return null; } 
    public EventDescriptorCollection GetEvents(Attribute[] attributes) { return EventDescriptorCollection.Empty; } 
    public EventDescriptorCollection GetEvents() { return EventDescriptorCollection.Empty; } 
    public PropertyDescriptorCollection GetProperties() { return GetProperties(null); } 
    public object GetPropertyOwner(PropertyDescriptor pd) { return this; } 

    public PropertyDescriptorCollection GetProperties(Attribute[] attributes) 
    { 
     var pds = 
      this.Keys 
      .Select(x => new DictionaryPropertyDescriptor(x)) 
      .ToArray(); 
     return new PropertyDescriptorCollection(pds); 
    } 

    #endregion 
} 

class DictionaryPropertyDescriptor : PropertyDescriptor 
{ 
    public DictionaryPropertyDescriptor(string name) : base(name, null) { } 
    public override bool CanResetValue(object component) { return false; } 
    public override Type ComponentType { get { return null; } } 
    public override bool IsReadOnly { get { return false; } } 
    public override Type PropertyType { get { return typeof(object); } } 
    public override void ResetValue(object component) { } 
    public override bool ShouldSerializeValue(object component) { return false; } 

    public override object GetValue(object component) 
    { 
     var dic = component as DictionaryDataObject; 
     if (dic == null) return null; 
     return dic[Name]; 
    } 

    public override void SetValue(object component, object value) 
    { 
     var dic = component as DictionaryDataObject; 
     if (dic == null) return; 
     dic[Name] = value; 
    } 
} 

樣品物體設置從後面的代碼:

DictionaryDataObject ddo = new DictionaryDataObject(); 

public Window4() 
{ 
    ddo["propa"] = 1; 
    ddo["propb"] = "foo"; 
    ddo["propc"] = "bar"; 
    ddo["propd"] = 4.5; 
    InitializeComponent(); 
    DataContext = ddo; 
} 

XAML用法:

<Window.Resources> 
    <DataTemplate x:Key="template"> 
     <WrapPanel> 
      <TextBlock Text="{Binding propa}" Margin="5"/> 
      <TextBlock Text="{Binding propb}" Margin="5"/> 
      <TextBlock Text="{Binding propc}" Margin="5"/> 
      <TextBlock Text="{Binding propd}" Margin="5"/> 
     </WrapPanel> 
    </DataTemplate> 
</Window.Resources> 
<Grid> 
    <ContentControl Content="{Binding}" ContentTemplate="{StaticResource template}"/> 
</Grid> 

如果你想ADAP將此解決方案轉換爲列表而不是字典,則必須根據list元素的某些屬性設置每個屬性名稱。

+0

謝謝,我會試試看。看起來似乎比我希望的複雜一點,但如果它很好地知道有一個工作。歡呼聲 – 2010-01-11 02:06:48