[英]WPF / C#, Adding rows to GridView after adding columns
I can't for the life of me figure out how to add rows to a ListView. 我一辈子都无法弄清楚如何向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?
我不明白为什么它对于ListBox来说很好(没有行)时为什么不起作用..我觉得我在这里缺少了一些非常简单的东西,有人可以帮我吗?
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>
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. 如注释部分中所建议,将
ListView
(尽管您的代码看起来像您针对DataGrid
) DataGrid
到某个集合,它将为集合中的每个元素创建一个“行”。
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) 另外,您需要将窗口的
DataContext
设置为包含您的集合的类(称为ViewModel)
<Window.DataContext>
<local:MyViewModel/>
</Window.DataContext>
If your not familar with MVVM or Bindings, here are some links to get you started 如果您不熟悉MVVM或绑定,可以通过以下链接开始使用
MVVM: MVVM:
http://msdn.microsoft.com/en-us/magazine/dd419663.aspx 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/36545/WPF-MVVM-Model-View-View-Model-Simplified
http://www.codeproject.com/Articles/165368/WPF-MVVM-Quick-Start-Tutorial http://www.codeproject.com/Articles/165368/WPF-MVVM-Quick-Start-Tutorial
Bindings: 绑定:
http://www.codeproject.com/Articles/140621/WPF-Tutorial-Concept-Binding http://www.codeproject.com/Articles/140621/WPF-Tutorial-Concept-Binding
http://msdn.microsoft.com/en-us/library/ms752347.aspx http://msdn.microsoft.com/en-us/library/ms752347.aspx
Good luck 祝好运
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.