下载自:https://github.com/eaplatt3/Lab-ListView
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace Lab_ListView
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void AddBtn_Click(object sender, RoutedEventArgs e)
{
for(int ii = 0; ii < 100; ii++)
{
VideoGameListView.Items.Add("Hello " + ii.ToString());
}
/*string tempPrice;
VideoGame vg = new VideoGame();
vg.GameName = GameNameTextBox.Text;
vg.Rating = RatingTextBox.Text;
tempPrice = PriceTextBox.Text;
if (!double.TryParse(tempPrice, out double i)) { }
vg.Price = i;
VideoGameListView.Items.Add(vg);
GameNameTextBox.Text = " ";
RatingTextBox.Text = " ";
PriceTextBox.Text = " ";
GameNameTextBox.Focus();*/
}
}
}
如果我使用的代码标有/* 和 */,它将添加项目到listView控件VideoGameListView没有问题,但当我使用循环添加项目时,它看起来像是在添加项目,运行应用程序时,listView中的滚动向下移动,但它没有显示任何内容。
xaml控制代码:
<Window x:Class="Lab_ListView.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Lab_ListView"
mc:Ignorable="d"
Title="Video Game List" Height="450" Width="800">
<Grid Margin="0,0,10,10">
<Label Content="Game Name" HorizontalAlignment="Left" Margin="40,59,0,0" VerticalAlignment="Top"/>
<TextBox x:Name="GameNameTextBox" HorizontalAlignment="Left" Height="23" Margin="121,62,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="366"/>
<Button x:Name="AddBtn" Content="Add" HorizontalAlignment="Left" Margin="121,151,0,0" VerticalAlignment="Top" Width="75" Click="AddBtn_Click"/>
<ListView x:Name="VideoGameListView" HorizontalAlignment="Left" Height="166" Margin="40,203,0,0" VerticalAlignment="Top" Width="447">
<ListView.View>
<GridView>
<GridViewColumn Header="Game Name" DisplayMemberBinding="{Binding GameName}"/>
<GridViewColumn Header="Rating" DisplayMemberBinding="{Binding Rating}"/>
<GridViewColumn Header="Price" DisplayMemberBinding="{Binding Price}"/>
</GridView>
</ListView.View>
</ListView>
<Label Content="Rating" HorizontalAlignment="Left" Margin="40,90,0,0" VerticalAlignment="Top"/>
<Label Content="Price" HorizontalAlignment="Left" Margin="40,121,0,0" VerticalAlignment="Top"/>
<TextBox x:Name="PriceTextBox" HorizontalAlignment="Left" Height="23" Margin="121,123,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="366"/>
<TextBox x:Name="RatingTextBox" HorizontalAlignment="Left" Height="23" Margin="121,92,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="366"/>
</Grid>
</Window>
1条答案
按热度按时间mrphzbgm1#
您已在XAML中指定列绑定到名为
GameName
、Rating
和Price
的项的属性。当您向列表中添加VideoGame
对象时,这些对象的属性将显示在这些列中。如果您只是添加string
,则没有此类属性。如果你将你的列绑定到特定的属性,那么这些属性必须存在于你添加的项目中,或者在这些项目的列中不显示任何内容。