0

I can't for the life of me figure out how to add rows to a ListView. I don't understand why this doesn't work when it works just fine for ListBox (without the rows).. i feel like i'm missing something really simple here, can someone help me out?

EDIT: didn't paste all the code sorry

<ListView Margin="10" Name="lvUsers">
        <ListView.View>
                <GridView>
                        <GridViewColumn Header="Name" Width="120" />
                        <GridViewColumn Header="Age" Width="50" />
                        <GridViewColumn Header="Mail" Width="150" />
                </GridView>
        </ListView.View>


<StackPanel Orientation="Horizontal" Height="45"> <!--Stacks Items Horizontally-->
                            <ComboBox Width="100" Height="30">
                                <ComboBoxItem IsSelected="True">DirecTV</ComboBoxItem>
                                <ComboBoxItem>Hyundai</ComboBoxItem>
                                <ComboBoxItem>None</ComboBoxItem>
                            </ComboBox>
                            <TextBox Width="445" Height="30" Text="Follow RedZone on Twitter" VerticalContentAlignment="Center"/>
                            <CheckBox IsChecked="True" VerticalAlignment="Center">
                                <CheckBox.LayoutTransform>
                                    <ScaleTransform ScaleX="1.5" ScaleY="1.5"></ScaleTransform>
                                </CheckBox.LayoutTransform>
                            </CheckBox>
                        <Button Content="Delete"  Height="Auto" Width="Auto" HorizontalAlignment="Right" VerticalAlignment="Top" VerticalContentAlignment="Top"/>
                    </StackPanel>
</ListView>
3
  • 2
    You don't "add rows" to a GridView. You BIND the ItemsSource to some relevant collection of data items. and let WPF handle the UI elements. Commented Sep 25, 2013 at 17:08
  • Where is the Listview here? Commented Sep 25, 2013 at 17:10
  • ah I didn't know that HighCore. I guess that's all i needed to know. thought i could add a row by using ListViewItem or something Commented Sep 25, 2013 at 17:19

1 Answer 1

1

As suggested in the comment section, Bind your ListView (although by your code looks like you aiming for a DataGrid) to some collection and it will create an "row" for every element in the collection.

You can define columns and bind them to properties of your elements

    <DataGrid Margin="10" Name="lvUsers" ItemsSource="{Binding YourCollection}">
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding Name}"/>
            <DataGridTextColumn Binding="{Binding Age}"/>
            <DataGridTextColumn Binding="{Binding Mail}"/>
        </DataGrid.Columns>    
    </DataGrid>

Also, you need to set the DataContext of the window to the class containing your collection (Known as ViewModel)

<Window.DataContext>
   <local:MyViewModel/>
</Window.DataContext>

If your not familar with MVVM or Bindings, here are some links to get you started

MVVM:

http://msdn.microsoft.com/en-us/magazine/dd419663.aspx

http://www.codeproject.com/Articles/36545/WPF-MVVM-Model-View-View-Model-Simplified

http://www.codeproject.com/Articles/165368/WPF-MVVM-Quick-Start-Tutorial

Bindings:

http://www.codeproject.com/Articles/140621/WPF-Tutorial-Concept-Binding

http://msdn.microsoft.com/en-us/library/ms752347.aspx

Good luck

Sign up to request clarification or add additional context in comments.

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.