列表框多列wpf

时间:2014-04-17 18:20:19

标签: wpf vb.net listbox

我目前有一个绑定到数据库的工作列表框,但我正在尝试将另一列添加到列表框但现在它不起作用。我错过了什么?

正常工作并填充Listbox,

<ListBox HorizontalAlignment="Left" ItemsSource="{Binding Source={StaticResource ListViewDataCurCmd}}" Margin="67,36,0,41" Name="lvwCurrCmd" SelectionMode="Multiple" Width="110" />

不工作,没有任何人填充

   <ListBox HorizontalAlignment="Left" ItemsSource="{Binding Source={StaticResource ListViewDataCurCmd}}" Margin="67,36,0,41" Name="lvwCurrCmd" SelectionMode="Multiple" Width="110">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid HorizontalAlignment="Stretch" >
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto" />
                        </Grid.ColumnDefinitions>
                        <TextBlock Grid.Column="0" Text="{Binding Source={StaticResource ListViewDataCurCmd}}" />
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

也许我不太了解如何填充列,只有整个列表框。我们是按列填充还是创建文本块以用作列表框中的“行”?

编辑: 我如何在xaml中声明我的Observable Collections以及它在

中填充的类
xaml
 <Window.Resources>

        <CollectionViewSource 
              Source="{Binding Source={x:Static Application.Current}, Path=AllCmd}"   
              x:Key="ListViewDataAllCmd" />

        <CollectionViewSource 
              Source="{Binding Source={x:Static Application.Current}, Path=CurCmd}"   
              x:Key="ListViewDataCurCmd" />
 </Window.Resources>


DataBinding.xaml.vb
Private AllCmd_Renamed As New ObservableCollection(Of String)()
    Private CurCmd_Renamed As New ObservableCollection(Of String)()
    Private fieldToReturn As New ObservableCollection(Of String)()
    Private valueToReturn As New ObservableCollection(Of String)()


    Public Sub AppStartup(ByVal sender As Object, ByVal args As StartupEventArgs)
        LoadAllCommandsDS()
        Dim mainWindow As New MainWindow()
        'mainWindow.Show()
    End Sub

    Private Sub LoadAllCommandsDS()
        Dim sSql As String = "SELECT * FROM Steps"
        Dim ds As DataSet
        Dim _List As String = ""
        ds = SQL(sSql)

        For i = 0 To ds.Tables(0).Rows.Count - 1
            _List = ds.Tables(0).Rows.Item(i).Item(1).ToString
            Me.AllCmd.Add(_List)
        Next
    End Sub

    Public Property AllCmd() As ObservableCollection(Of String)
        Get
            Return Me.AllCmd_Renamed
        End Get
        Set(ByVal value As ObservableCollection(Of String))
            Me.AllCmd_Renamed = value
        End Set
    End Property

    Public Property CurCmd() As ObservableCollection(Of String)
        Get
            Return Me.CurCmd_Renamed
        End Get
        Set(ByVal value As ObservableCollection(Of String))
            Me.CurCmd_Renamed = value
        End Set
End Property

2 个答案:

答案 0 :(得分:1)

你不应该在这里重复来源 ListBox有源

<TextBlock Grid.Column="0" Text="{Binding Source={StaticResource ListViewDataCurCmd}}" />

你应该只有一个通往该属性的路径

<TextBlock Grid.Column="0" Text="{Binding Path=PropertyName}" />

答案 1 :(得分:0)

试试这个:

<Grid HorizontalAlignment="Stretch" >
          <Grid.ColumnDefinitions>
               <ColumnDefinition Width="Auto" />
               <ColumnDefinition Width="Auto" />
                       ...
          </Grid.ColumnDefinitions>
          <TextBlock Grid.Column="0" Text="{Binding Source={StaticResource ListViewDataCurCmd}, Path=PropertyName1}" />
          <TextBlock Grid.Column="1" Text="{Binding Source={StaticResource ListViewDataCurCmd}, Path=PropertyName2}" />
                      ....
</Grid>