2012-03-16 56 views
0

我想知道是否有可能將datagrid列綁定到存儲在可觀察集合中的列表(Of T)!綁定到一個集合內的列表WPF/VB

這裏是我當前的代碼:

Dim _BindWithThis As New List(Of BindWithThis) 
    Me.DataContext = _BindWithThis 

    For i = 0 To 3 
     Dim NewList As New List(Of Double) From {i + 0.25, i + 0.5, i + 0.75} 
     _BindWithThis.Add(New BindWithThis() With _ 
    {.InternalNum = i, .DataValue = NewList}) 
    Next 

    DataGrid1.ItemsSource = _BindWithThis 

    Dim NC As New DataGridTextColumn 
    NC.Header = "New Column" 
    NC.Width = 85 
    Dim b As New Binding 
    b.Path = New PropertyPath("DataValue") 
    NC.Binding = b 

    DataGrid1.Columns.Add(NC) 

這當前顯示的 「(集合)」 四行。是否有可能進入這些「收集」行之一併顯示數據?我知道,這是可能的列表框做的集合中與特定的元件結合:

 ListBox1.ItemsSource = _BindWithThis.Item(0).DataValue 

我只是不知道如何與一個DataGrid做到這一點...

謝謝你的幫助!

詹姆斯

+0

您需要調查DataGrid.RowDetailsTemplate。如果沒有人打敗我,我會在後面剔除一個例子。 – Phil 2012-03-16 12:18:08

回答

1

這是我承諾的例子。這使用DataGrid.RowDetailsTemplate來允許您展開/摺疊數據列表。道歉,它是C#而不是VB.NET。

的XAML:

<Page.DataContext> 
    <Samples:DataGridRowDetails2ViewModel/> 
</Page.DataContext> 

<Grid> 
    <DataGrid x:Name="dataGrid" ItemsSource="{Binding Items}" 
       AutoGenerateColumns="False" IsReadOnly="True"> 
     <DataGrid.Columns> 
      <DataGridTextColumn Binding="{Binding Name}" Header="Name" /> 
      <DataGridTemplateColumn Header="Show details"> 
       <DataGridTemplateColumn.CellTemplate> 
        <DataTemplate> 
         <ToggleButton Content="Show details" 
             IsChecked="{Binding IsChecked}" 
             Checked="ToggleButtonChecked" Unchecked="ToggleButtonChecked"/> 
        </DataTemplate> 
       </DataGridTemplateColumn.CellTemplate> 
      </DataGridTemplateColumn> 
     </DataGrid.Columns> 
     <DataGrid.RowDetailsTemplate> 
      <DataTemplate DataType="{x:Type Samples:DataItemWithDetails}"> 
       <ItemsControl ItemsSource="{Binding Doubles}" /> 
      </DataTemplate> 
     </DataGrid.RowDetailsTemplate> 
    </DataGrid>  
</Grid> 

C#

public partial class DataGridRowDetails2 
{ 
    public DataGridRowDetails2() 
    { 
     InitializeComponent(); 
    } 

    private void ToggleButtonChecked(object sender, RoutedEventArgs e) 
    { 
     var button = (ToggleButton)sender; 

     DataGridRow dataGridRow = 
      (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromItem(button.DataContext); 
      dataGridRow.DetailsVisibility = 
       (button.IsChecked??false) ? Visibility.Visible : Visibility.Collapsed; 
    } 
} 

當然使用的ObservableCollection <>和您會執行INotifyPropertyChanged真正的代碼。

public class DataGridRowDetails2ViewModel 
{ 
    public DataGridRowDetails2ViewModel() 
    { 
     Items = new List<DataItemWithDetails> 
        { 
         new DataItemWithDetails{ Name = "Item 1"}, 
         new DataItemWithDetails{ Name = "Item 2"}, 
         new DataItemWithDetails{ Name = "Item 3"}, 
         new DataItemWithDetails{ Name = "Item 4"}, 
        }; 
    } 

    public IList<DataItemWithDetails> Items { get; set; } 

    public bool IsChecked { get; set; } 
} 

public class DataItemWithDetails 
{ 
    public DataItemWithDetails() 
    { 
     Doubles = new List<double> {1, 2, 3, 4}; 
    } 

    public string Name { get; set; } 

    public IList<double> Doubles { get; set; } 
} 
0

您需要設置AutoGenerateColumns="False"和定義列你想自己

這裏有一個簡單的例子,這將在ListBox代替,而不是默認顯示您的收藏TextBlock

<DataGrid AutoGenerateColumns="False" 
      ItemsSource="{Binding YourCollection}"> 
    <DataGrid.Columns>   
     <DataGridTemplateColumn Header="Column Header"> 
      <DataGridTemplateColumn.CellTemplate> 
       <DataTemplate> 
        <ListBox ItemsSource="{Binding MySubCollection}" /> 
       </DataTemplate> 
      </DataGridTemplateColumn.CellTemplate> 
     </DataGridTemplateColumn> 
    </DataGrid.Columns> 
</DataGrid>