我想顯示所選項目的總價值,所選項目在ListView中。項目的值出現在列表中,但我希望項目的總值在文本框內顯示。我的代碼如下:如何添加一列ListView
Basket.xaml
private static ObservableCollection<Menu.PassedData> passedData = new ObservableCollection<Menu.PassedData>();
private double totalValue;
public double TotalValue
{
get { return totalValue; }
set
{
totalValue = value;
OnPropertyChanged("TotalValue");
}
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
Menu.PassedData data = e.Parameter as Menu.PassedData;
if (data != null) //If data is not 0
{
PassedData.Add(data); //Increment data in list view
double tempTotalValue = 0;
foreach (var record in PassedData)
{
tempTotalValue = tempTotalValue + record.Value;
}
TotalValue = tempTotalValue;
}
}
}
其中值存儲(Basket.xaml)名單
<ListBox x:Name="listBox1" HorizontalAlignment="Left" Height="265" Margin="278,175,0,0" VerticalAlignment="Top" Width="112">
<ListView x:Name="PriceView" ItemsSource="{Binding PassedData}" HorizontalAlignment="Left" VerticalAlignment="Center">
<ListView.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<!--<TextBox Grid.Column="0" Text="{Binding Name}" IsReadOnly="True" /> !-->
<TextBox Grid.Column="1" Text="{Binding Value}" IsReadOnly="True" />
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ListBox>
所以基本上加起來值列在列表視圖中並將其顯示在文本框中。
謝謝你的回答。我已經實現了你的答案,但是當我運行應用程序時,無論我添加項目的項目多少,總文本塊保持靜態只顯示0。問題是更新。 –
你的'Basket'類是否實現'INotifyPropertyChanged'接口? – SteppingRazor
不是什麼? –