绑定到集合WPF / VB中的列表

时间:2012-03-16 10:49:29

标签: wpf vb.net datagrid wpfdatagrid

我想知道是否可以将datagrid列绑定到存储在可观察集合中的列表(Of T)!

这是我目前的代码:

    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)

目前显示四行“(收藏)”。是否可以进入其中一个“Collection”行并显示数据?我知道这可以通过与集合中的特定元素绑定来实现列表框:

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

我无法解决如何使用数据网格...

感谢您的帮助!

詹姆斯

2 个答案:

答案 0 :(得分: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&lt;&gt;并为实际代码实现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; }
}

答案 1 :(得分: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>