c# - Foreach 仅适用于 ListBox 的第一项

标签 c# foreach listbox windows-phone

我的 Windows Phone 应用程序有一个从 JSON 填充的列表框。填充 ListBox 后,我需要比较用户键入的内容(字符串)是否存在于 ListBox 中。 因此,ListBox 上的每个项目都有 3 个信息,来自一个集合。我只需要将集合中的一项与字符串进行比较。

我有一些代码,但这只适用于 ListBox 的第一项:

private void addProduto(object sender, RoutedEventArgs e)
{            
    Fields fi = (Fields)this.List1.SelectedItem as Fields;

    foreach(var item in List1.Items)
    {
        Fields fields = item as Fields;

        if (fields.codigo == insCodProduto.Text)
        {
            MessageBox.Show("this product exists!");
            break;
        }
        else
        {
            MessageBox.Show("this product not exists");
            break;
        }
    }
}

收藏:

public ObservableCollection<Fields> Items { get; set; }

public class Fields
{
    public string descricao { get; set; }
    public double valor_preco_a { get; set; }
    public string codigo { get; set; }
    public string codigo1 { get; set; }
    public string descricao1 { get; set; }
}

列表框:

<ListBox Name="List1" ItemsSource="{Binding Items}" Margin="0,85,0,0" >
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="242" />
                    <ColumnDefinition Width="128" />
                    <ColumnDefinition Width="Auto" />
                </Grid.ColumnDefinitions>
                <StackPanel Hold="addToList2" Margin="0,0,-62,17" Grid.ColumnSpan="3">
                    <StackPanel.Background>
                        <SolidColorBrush Color="#FF858585" Opacity="0.5"/>
                    </StackPanel.Background>
                    <TextBlock x:Name="NameTxt" Grid.Column="0" Text="{Binding descricao}" TextWrapping="Wrap" FontSize="20" Style="{StaticResource PhoneTextNormalStyle}"/>
                    <TextBlock Grid.Column="1" Text="{Binding valor_preco_a, StringFormat=N2}" TextWrapping="Wrap" Margin="45,20,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
                    <TextBlock Grid.Column="3" Text="{Binding codigo}" TextWrapping="Wrap" FontSize="35" Margin="370,-50,12,0" Style="{StaticResource PhoneTextNormalStyle}" Foreground="Blue"/>
                </StackPanel>
                <TextBlock Grid.Column="0" Text="R$" Margin="15,48,158,17" Style="{StaticResource PhoneTextSubtleStyle}"/>
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>

最佳答案

由于 break 语句,您的 foreach 仅检查第一个值。此外,循环需要检查值是否匹配,但是您只能在循环完成后才能知道是否没有匹配。所以你需要这样的东西。

bool found = false;
foreach(Fields fields in List1.Items)
{
    if (fields.codigo == insCodProduto.Text)
    {
        MessageBox.Show("this product exists!");
        found = true;
        break;
    }
}

if(!found)
{
    MessageBox.Show("this product not exists");
}

另请注意,如果您在 foreach 中指定类型,它将为您进行转换。

关于c# - Foreach 仅适用于 ListBox 的第一项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26485771/

相关文章:

python - 如何滚动不活动的 Tkinter 列表框?

c# - 如何返回对尚不存在的对象的引用?

c# - 在 winrt c# 中加密字符串并在 c# .net 中解密

c# - MongoDB:具有重写 ID 和属性的子类出现问题

c# - WPF Xaml Designer - 在设计时处理资源

r - 为什么 save 和 saveRDS 在 dopar 中的行为不同?

Java foreach递归返回(树中的 child 数)

lambda - 在c++中混合每个和lambdas

c# - WPF 列表框选择已更改

dictionary - 如何在列表框中显示字典