2012-05-05 4 views
2

이것은 내가 온라인으로 정보를 찾을 수 없었던 흥미로운 사례입니다. 표를 만들고 ObservableCollection의 ObservableCollection을 바인딩해야합니다. 이와 같은 모델을 상상해 : ObservableCollection의 ObservableCollection 그리드

public class App 
{ 
    private ObservableCollection<MyNewCollection> collections; 
} 

public class MyNewCollection : DependencyObject 
{ 
    public ObservableCollection<MyCollectionItem> items; 
    // ... public properties: string CollectionTitle 
} 

public class MyCollectionItem : DependencyObject 
{ 
    // ... public properties: string ItemTitle 
} 

나는 모든 행이 컬렉션 ObservableCollections에있는 항목 중 하나에서 CollectionTitle를 포함 할 수 있도록 개체를 컬렉션의 항목을 나열하는 그리드의 첫 번째 열을 원한다. 두 번째 열에는 각 행에 적절한 컬렉션 개체와 관련된 MyCollectionItems 항목 집합이 들어 있어야합니다. 위의 코드에서

: 'C'로

  1. 컬렉션
  2. '내가'나는 MyNewCollection의 정적 세트가 있다면

 
+---------------------------------------------------------------------------------------------+ 
+  |  Column 0   |      Column 1       | 
+---------------------------------------------------------------------------------------------| 
+ Row 0 | c[0].CollectionTitle | c[0].i[0].ItemTitle ...i[1].ItemTitle ... i[2].ItemTitle | 
+ Row 1 | c[1].CollectionTitle | c[1].i[0].ItemTitle ...i[1].ItemTitle ... i[2].ItemTitle | 
+  |       |               | 
+ ...                       | 
+---------------------------------------------------------------------------------------------+ 
쉽게했을로
  • 항목 개체가 있지만 무한대로 성장할 수 있기 때문에 MyNewCollection 개체의 ObservableCollection을 새로 만들어야합니다.이 개체는 WPF에서이 작업을 수행하는 방법을 이해하는 데 어려움을 겪고 있습니다. 어떤 도움을 주시면 감사하겠습니다.

    감사합니다.

  • 답변

    2

    각 행에 다른 ItemsControl이있는 ItemsControl을 사용하는 한 가지 방법이 있습니다.

    XAML :

    <Page.Resources> 
        <DataTemplate x:Key="ChildItemTemplate" 
            DataType="{x:Type Samples:NestedCollectionItem}"> 
         <TextBlock Text="{Binding Title}" Margin="5"/> 
        </DataTemplate> 
        <ItemsPanelTemplate x:Key="ChildItemPanel"> 
         <StackPanel Orientation="Horizontal"/> 
        </ItemsPanelTemplate> 
        <DataTemplate x:Key="ItemTemplate" 
            DataType="{x:Type Samples:NestedCollectionChildViewModel}"> 
         <Grid> 
          <Grid.ColumnDefinitions> 
           <ColumnDefinition Width="Auto" SharedSizeGroup="c1"/> 
           <ColumnDefinition /> 
          </Grid.ColumnDefinitions> 
          <TextBlock VerticalAlignment="Center" Text="{Binding Title}"/> 
          <ItemsControl 
           Grid.Column="1" 
           ItemsSource="{Binding Items}" 
           ItemsPanel="{StaticResource ChildItemPanel}" 
           ItemTemplate="{StaticResource ChildItemTemplate}" 
           /> 
         </Grid> 
        </DataTemplate> 
    </Page.Resources> 
    
    <Page.DataContext> 
        <Samples:NestedCollectionRootViewModel/> 
    </Page.DataContext> 
    
    <Grid> 
        <ItemsControl 
         Grid.IsSharedSizeScope="True" 
         ItemsSource="{Binding Items}" 
         ItemTemplate="{StaticResource ItemTemplate}"/> 
    </Grid> 
    

    코드 :

    public class NestedCollectionRootViewModel 
    { 
        public NestedCollectionRootViewModel() 
        { 
         Items = 
          new ObservableCollection<NestedCollectionChildViewModel> 
           { 
            new NestedCollectionChildViewModel 
             { 
              Title = "Item 1", 
              Items = 
               new ObservableCollection<NestedCollectionItem> 
                { 
                 new NestedCollectionItem {Title = "One"}, 
                 new NestedCollectionItem {Title = "Two"}, 
                 new NestedCollectionItem {Title = "Three"}, 
                 new NestedCollectionItem {Title = "Four"}, 
                } 
             }, 
    
            new NestedCollectionChildViewModel 
             { 
              Title = "Item 2", 
              Items = 
               new ObservableCollection<NestedCollectionItem> 
                { 
                 new NestedCollectionItem {Title = "Five"}, 
                 new NestedCollectionItem {Title = "Six"}, 
                } 
             }, 
    
           }; 
        } 
    
        public ObservableCollection<NestedCollectionChildViewModel> Items 
         { get; private set; } 
    } 
    
    public class NestedCollectionChildViewModel 
    { 
        public string Title { get; set; } 
        public ObservableCollection<NestedCollectionItem> Items { get; set; } 
    } 
    
    public class NestedCollectionItem 
    { 
        public string Title { get; set; } 
        // ... etc 
    } 
    
    +0

    놀라운 - 매일 WPF와 사랑에 빠지다. 감사합니다 필! – Shawn

    0

    가 난 단지 간단히 C 번호와 함께 일하지만, 내가 정확히 기억한다면, 당신은 직접 결합 할 수 없을 때, 당신이 작성해야 사용자 정의 ValueConverter 클래스는 MyCollectionItem 요소를 처리 한 다음 바인딩에 사용합니다.

    관련 문제