0

I currently have a working listbox that is binded to a database, but I'm trying to add another column to the listbox but now it doesn't work. What am I missing?

Working and populates Listbox fine,

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

Not working and nothing populates

   <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>

Perhaps I don't quite understand how to populate columns, only the whole listbox. Do we populate by column or are we creating textblocks to use as 'rows' in the listbox?

Edit: How I declare my Observable Collections in xaml as well as the class it's populated in

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
  • Use a ListView instead, which has built-in support for columns Commented Apr 17, 2014 at 18:23
  • Ok, I am researching how to move to a Listview but have a question, how would I access the ObservableCollection I am using currently for my ListBox(is created and instantiated in a class called DataBinding, all examples I see reference the DisplayMemberBinding from the property created in the same forms' .xaml.vb code Commented Apr 17, 2014 at 18:49

2 Answers 2

1

You should NOT repeat the source here
The ListBox has the source

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

You should just have a path to that property

<TextBlock Grid.Column="0" Text="{Binding Path=PropertyName}" />
Sign up to request clarification or add additional context in comments.

6 Comments

Ok, the property I use to populate the ObservableCollection is on another class, would I still be able to use the Path=PropertyName is the property was created on another class?
What? You have an ObservableCollection<T> Path needs to be the property name in T.
I've updated my post to show how I declare my collections, perhaps I'm misunderstanding how this binding thing works
Just where do you expect to get the value for second column when you have a OC of string?
My original plan was to add values to another collection depending on what button the user selects and assign that collection to the second column in the listview but this is proving troublesome
|
0

Try this:

<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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.