我有一个包含三个值的列表框。我需要为ListBox的所有项获取FEstado
值并求它们。但是,此ListBox没有已定义的项目,用户从另一个ListBox中选择项目。
我的列表框:
<ListBox Name="List2" HorizontalContentAlignment="Stretch" Grid.ColumnSpan="3" Margin="0,43,-66,0">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0,0,0,17" Width="432" Height="95" Hold="holdListRmv">
<TextBlock Grid.Column="0" Text="{Binding FNome}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
<TextBlock Grid.Column="1" Text="{Binding FEstado}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
<TextBlock Grid.Column="2" Text="{Binding Quantity}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextNormalStyle}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
答案 0 :(得分:2)
假设您通过将模型项添加到ListBox
的{{1}}属性(如your previous question所示)来填充ListBox
,那么您可以获取所有内容来自同一Items
属性的项目。假设ListBox.Items
是一个数字,你可以这样做:
FEstado
答案 1 :(得分:1)
你可以这样做, 试试这个:
XAML:
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="0">
<StackPanel>
<ListBox Name="List2" HorizontalContentAlignment="Stretch" Grid.ColumnSpan="3" Margin="0,43,-66,0">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0,0,0,17" Width="432" Height="95" >
<TextBlock Grid.Column="0" Text="{Binding FNome}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
<TextBlock Grid.Column="1" Text="{Binding FEstado}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
<TextBlock Grid.Column="2" Text="{Binding Quantity}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextNormalStyle}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button Content="sum" Click="Button_Click_1"></Button>
</StackPanel>
</Grid>
CS:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
ObservableCollection<DataClass> obj = new ObservableCollection<DataClass>();
obj.Add(new DataClass("AA", "10", "10"));
obj.Add(new DataClass("BB", "10", "10"));
List2.ItemsSource = obj;
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
int sum = 0;
for (int i = 0; i < List2.Items.Count; i++)
{
DataClass obj = (DataClass)List2.Items[i];
sum += int.Parse(obj.FEstado.ToString());
}
MessageBox.Show(sum.ToString());
}
public class DataClass
{
public string FNome { get; set; }
public string FEstado { get; set; }
public string Quantity { get; set; }
public DataClass() { }
public DataClass(string FNome, string FEstado, string Quantity)
{
this.FNome = FNome;
this.FEstado = FEstado;
this.Quantity = Quantity;
}
}