我正在使用WPF制作一个小应用程序,将两个测量值(以英寸为单位)转换为最终的平方英尺答案。然后在我点击"计算"按钮,它应该将信息添加到ListView
。到目前为止,我能够计算出最终的平方英尺,然后在点击“计算”之后将一行信息添加到列表中。按钮无法再添加到ListView
。如何将多行不同信息添加到ListView
?
C#:
public partial class MainWindow : Window
{
List<Dimension> dimensionList = new List<Dimension>();
public MainWindow()
{
InitializeComponent();
}
private void calculate_bttn_Click(object sender, RoutedEventArgs e)
{
Calculate();
}
private void Calculate()
{
//----get height from textbox-------------------print height to console----//
int height = Int32.Parse(height_TB.Text); Console.WriteLine("Height: " + height);
//----get width from textbox--------------------print width to console----//
int width = Int32.Parse(width_TB.Text); Console.WriteLine("Widht: " + width);
//----converting square inches into square feet----//
int toSquareInches = height * width;
int toSquareFeet = toSquareInches / 144;
//----create new dimension----//
Dimension dim = new Dimension();
//----set dimension properties----////
dim.height = height;
dim.width = width;
dim.sqrInches = toSquareInches;
dim.sqrFeet = toSquareFeet;
//----add new dimension to dimensionList----//
dimensionList.Add(dim);
//----set binding source for listview----//
listView.ItemsSource = dimensionList;
//----display final square footage----//
total_label.Content = "Total: " + toSquareFeet.ToString() + " square feet";
}
}
class Dimension
{
public int height { get; set; }
public int width { get; set; }
public int sqrInches { get; set; }
public int sqrFeet { get; set; }
public override string ToString()
{
return "height:" + height.ToString() + " width:" + width.ToString() + " Sqr inches:" + sqrInches.ToString() + " Sqr Feet:" + sqrFeet.ToString(); ;
}
}
XAML:
<Window x:Class="Conversion.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:Conversion"
mc:Ignorable="d"
Title="Converter" Height="337.333" Width="397.667">
<Grid Margin="0,0,2,0">
<TextBox x:Name="height_TB" HorizontalAlignment="Left" Height="23" Margin="10,16,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
<TextBox x:Name="width_TB" HorizontalAlignment="Left" Height="23" Margin="10,44,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
<Label x:Name="total_label" Content="Total:" HorizontalAlignment="Left" Margin="10,97,0,0" VerticalAlignment="Top"/>
<Button x:Name="calcuate_bttn" Content="CALCULATE" HorizontalAlignment="Left" Margin="10,72,0,0" VerticalAlignment="Top" Width="120" Click="calculate_bttn_Click"/>
<Label x:Name="height_label" Content="height" HorizontalAlignment="Left" Margin="135,12,0,0" VerticalAlignment="Top"/>
<Label x:Name="width_label" Content="width" HorizontalAlignment="Left" Margin="135,40,0,0" VerticalAlignment="Top" RenderTransformOrigin="-0.111,-2.413"/>
<ListView x:Name="listView" HorizontalAlignment="Left" Height="168" Margin="10,128,0,0" VerticalAlignment="Top" Width="368">
<ListView.View>
<GridView>
<GridViewColumn/>
</GridView>
</ListView.View>
</ListView>
</Grid>
</Window>
答案 0 :(得分:1)
您的原始代码已添加多个项目,但未通知用户界面更改。显示的是一项,因为当您设置ItemsSource
时,它会导致属性更改为触发(从null
到dimensionList
)。在您的后续点击中,您设置了相同的集合(dimensionList
),该集合不会通知该属性已更改,因为它是同一个对象。
您可以使用ObservableCollection
,只要在集合中添加或删除新的Dimension
,就可以通知用户界面。
public partial class MainWindow : Window
{
ObservableCollection<Dimension> dimensionList = new ObservableCollection<Dimension>();
public MainWindow()
{
InitializeComponent();
//----set binding source for listview----//
listView.ItemsSource = dimensionList;
}
private void calculate_bttn_Click(object sender, RoutedEventArgs e)
{
Calculate();
}
private void Calculate()
{
//----get height from textbox-------------------print height to console----//
int height = Int32.Parse(height_TB.Text); Console.WriteLine("Height: " + height);
//----get width from textbox--------------------print width to console----//
int width = Int32.Parse(width_TB.Text); Console.WriteLine("Widht: " + width);
//----converting square inches into square feet----//
int toSquareInches = height * width;
int toSquareFeet = toSquareInches / 144;
//----create new dimension----//
Dimension dim = new Dimension();
//----set dimension properties----////
dim.height = height;
dim.width = width;
dim.sqrInches = toSquareInches;
dim.sqrFeet = toSquareFeet;
//----add new dimension to dimensionList----//
dimensionList.Add(dim);
//----display final square footage----//
total_label.Content = "Total: " + toSquareFeet.ToString() + " square feet";
}
}