2013-11-04 152 views
1

顯示觀察到的集合我需要在DataGridWPF MVVM在數據網格

ItemsCollectionAItemsCollectionBItemsCollectionC以顯示對象的所有信息都是ObservableCollections

我有一個DataGrid:

<DataGrid ItemsSource="{Binding Path=itemscollectionA}" HeadersVisibility="All" /> 

這顯示網格格式的所有屬性,但ItemsCollectionBItemsCollectionC被顯示爲(集合)。

enter image description here

我怎樣才能得到ItemsCollectionBItemsCollectionC向下進出擴大電網,以顯示其屬性以及

+0

這是因爲它不知道如何顯示這些集合。它基本上在你的ItemsCollectionB和ItemsCollectionC上調用'.ToString()'。我們需要更多信息來回答您的問題。正如@geedubb所問,你想爲這些顯示什麼? – gleng

+0

現在它告訴我有一個集合,我想要集合的每個項目中的所有值,即顯示集合中每個項目中存在的稱爲名稱或值的屬性(爲清晰起見編輯) – Oliver

+0

@奧利弗你**有**自動生成列?因爲如果不是這很容易 –

回答

1

好好像數據網格是完全錯誤的事情,我需要在這裏。我做了一個listbox的stackpanel,其中itemssource設置爲每個綁定,並且顯示正常

<StackPanel Background="white" HorizontalAlignment="Stretch" Margin="0"> 
     <ListBox Background="white" x:Name="BetsListBox" VerticalAlignment="Stretch" BorderThickness="0" 
        ItemsSource="{Binding Path=ItemsCollectionA}" Margin="0" Width="Auto" HorizontalAlignment="Stretch" > 
      <ListBox.Resources> 
       <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#F0F0F0"/> 
       <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="#F0F0F0"/> 
       <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black"/> 
      </ListBox.Resources> 
      <ListBox.ItemTemplate> 
       <DataTemplate> 
        <StackPanel > 
         <ListBox ItemsSource="{Binding Path=ItemsCollectionB}"> 
          <ListBox.ItemTemplate> 
           <DataTemplate> 
            <StackPanel Orientation="Horizontal"> 
             <TextBlockText="{Binding Path=varA}" /> 
             <TextBlockText="{Binding Path=varB}" /> 
             <TextBlockText="{Binding Path=varC}" /> 
            </StackPanel> 
           </DataTemplate> 
          </ListBox.ItemTemplate> 
         </ListBox> 
         <ListBox BorderThickness="0" ItemsSource="{Binding Path=ItemsCollectionC}" > 
          <ListBox.ItemTemplate> 
           <DataTemplate> 
            <StackPanel Orientation="Horizontal" > 
             <TextBlock Text="{Binding Path=VarA}" ToolTip="{Binding Name}"/> 
             <TextBlock Text="{Binding Path=VarB}" ToolTip="{Binding Name}"/> 
             <TextBlock Text="{Binding Path=VarC}" ToolTip="{Binding Name}"/> 
            </StackPanel> 
           </DataTemplate> 
          </ListBox.ItemTemplate> 
         </ListBox > 
        </StackPanel> 
       </DataTemplate> 
      </ListBox.ItemTemplate> 
     </ListBox> 
    </StackPanel> 
1

一個DataGrid中只能管理一個項目源。一切都是基於行的,列並不那麼聰明。

兩種方式:

  • 將數據合併成兩組領域的一個新的對象(將是最簡單)。
  • 並排同步2個數據網格。
3

如果您可以指定列而不是自動生成它們,這可能非常容易。

下面是一個例子:

<DataGrid ItemsSource="{Binding Employees}" AutoGenerateColumns="False"> 
    <DataGrid.Columns> 
     <DataGridTextColumn Binding="{Binding EmployeeName}"/> 
     <!-- Displays the items of the first collection--> 
     <DataGridTemplateColumn> 
      <DataGridTemplateColumn.CellTemplate> 
       <DataTemplate> 
        <ListBox ItemsSource="{Binding Dogs}"/> 
       </DataTemplate> 
      </DataGridTemplateColumn.CellTemplate> 
     </DataGridTemplateColumn> 

     <!-- Displays the items of the second collection--> 
     <DataGridTemplateColumn> 
      <DataGridTemplateColumn.CellTemplate> 
       <DataTemplate> 
        <ListBox ItemsSource="{Binding Cats}"/> 
       </DataTemplate> 
      </DataGridTemplateColumn.CellTemplate> 
     </DataGridTemplateColumn> 
    </DataGrid.Columns> 
</DataGrid> 

視圖模型:

public class MainWindowViewModel : NotificationObject 
{ 
    public MainWindowViewModel() 
    { 
     Employees = new ObservableCollection<Employee> 
     { 
      new Employee { EmployeeName = "Steven"}, 
      new Employee { EmployeeName = "Josh"}, 
     }; 
    } 

    public ObservableCollection<Employee> Employees { get; set; } 
} 

型號:

public class Employee 
{ 
    public Employee() 
    { 
     Dogs = new ObservableCollection<Dog> 
     { 
      new Dog { Gender = 'M'}, 
      new Dog { Gender = 'F'}, 
     }; 
     Cats = new ObservableCollection<Cat> 
     { 
      new Cat { Name = "Mitzy" , Kind = "Street Cat"}, 
      new Cat { Name = "Mitzy" , Kind = "House Cat"} 
     }; 
    } 

    public string EmployeeName { get; set; } 

    public ObservableCollection<Dog> Dogs { get; set; } 

    public ObservableCollection<Cat> Cats { get; set; } 
} 

public class Dog 
{ 
    public char Gender { get; set; } 

    public override string ToString() 
    { 
     return "Dog is a '" + Gender + "'"; 
    } 
} 

public class Cat 
{ 
    public string Name { get; set; } 

    public string Kind { get; set; } 

    public override string ToString() 
    { 
     return "Cat name is " + Name + " and it is a " + Kind; 
    } 
} 

考慮ItemsCollectionAEmployeesItemsCollectionBItemsCollectionCDogsCats。它仍然使用ToString來顯示我覆蓋的DogsCats對象,但是您可以簡單地將DataTemplate設置爲列中的列表框,以決定如何顯示模型。請注意0​​上的AutoGenerateColumns="False"以避免創建兩次列。

希望這有助於