在Datagrid中对一个observablecollection(observablecollection(of double))进行wpf绑定

时间:2016-01-28 14:26:26

标签: xml wpf vb.net binding datagrid

我正在尝试实现一个带有可变行数和列数的数据网格,这些行和列绑定到ObservableCollection(ObservableCollection(Double)

我希望将此集合与表示值的内部集合一起使用,以显示在外部集合包含数据网格行的行中。我还有一个observablecollection(of string),其中包含一组列标题。

有没有办法将标题绑定到标题集合,将数据绑定到二维数据数组?

| Title1 | Title2 | Title3 | Title4 |

| 11.00 | 20.00 | 45.00 | 100.00 | | ----------------------------------------------- | 1.00 | 230.00 | | | | -----------------------------------------------  等等...

我的视图模型如下:

Public Class MainWindowViewModel
   Inherits ViewModelBase

   Private _dgvData As ObservableCollection(Of ObservableCollection(Of Double)) = New ObservableCollection(Of ObservableCollection(Of Double))()
   Private _dgvTitles As ObservableCollection(Of String)

   Public Property DgvData As ObservableCollection(Of ObservableCollection(Of Double))
      Get
         Return _dgvData
      End Get
      Set(value As ObservableCollection(Of ObservableCollection(Of Double)))
         _dgvData = value
         RaisePropertyChanged(Function() DgvData)
      End Set
   End Property
   Public Property DgvTitles As ObservableCollection(Of String)
      Get
         Return _dgvTitles
      End Get
      Set(value As ObservableCollection(Of String))
         _dgvTitles = value
         RaisePropertyChanged(Function() DgvTitles)
      End Set
   End Property

   Public Sub New()
      PopulateDatagrid()
   End Sub

   Public Sub PopulateDatagrid()
      Dim oc As ObservableCollection(Of ObservableCollection(Of Double)) = New ObservableCollection(Of ObservableCollection(Of Double))()
      Dim row As ObservableCollection(Of Double) = New ObservableCollection(Of Double)()
      row.Add(11D)
      row.Add(20D)
      row.Add(45D)
      row.Add(100D)
      oc.Add(row)
      DgvData = New ObservableCollection(Of ObservableCollection(Of Double))(oc)

      Dim tc As ObservableCollection(Of String) = New ObservableCollection(Of String)()
      tc.Add("One")
      tc.Add("Two")
      tc.Add("Three")
      tc.Add("Four")
      DgvTitles = New ObservableCollection(Of String)(tc)
   End Sub
End Class

我的表格如下:

<Window x:Class="MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:TwoDimBindingProblem"
        Title="MainWindow"
        >

   <Window.DataContext>
      <local:MainWindowViewModel/>
   </Window.DataContext>
   <Grid>
      <StackPanel HorizontalAlignment="Left" 
                  VerticalAlignment="Top"
                  Orientation="Vertical">
         <DataGrid ItemsSource="{Binding DgvData}"
                   ColumnWidth="*"
                   CanUserAddRows="False"
                   AutoGenerateColumns="True">
            <DataGrid.Resources>
               <local:BindingProxy x:Key="BindingProxy" Data="{Binding}"/>
            </DataGrid.Resources>
            <DataGridTemplateColumn>
               <DataGridTemplateColumn.HeaderTemplate>
                  <DataTemplate>
                     <TextBlock Text="{Binding DgvTitles}"/>
                  </DataTemplate>
               </DataGridTemplateColumn.HeaderTemplate>
            </DataGridTemplateColumn>
         </DataGrid>
      </StackPanel>
    </Grid>
</Window>

我得到的只是一个看起来像这样的窗口。关于如何将标题集合绑定到列标题以及将二维observablecollection绑定到数据网格的任何想法?

Output WPF Form

0 个答案:

没有答案