0

我在 JSON 中获取数据并存储在 List by

List<Product> rootObject = JsonConvert.DeserializeObject<List<Product>>(e.Result);

之后,我在 ListBox 中显示数据

  productlist.ItemsSource = rootObject;

我的 xaml 文件:-

 <ListBox Height="600" HorizontalAlignment="Left" Margin="5,91,0,0" Name="productlist" VerticalAlignment="Top" Width="441" 
             SelectionChanged="productlistselectionchanged">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal" Height="132">
                    <!--    <Image Source="{Binding Path=http://callme4.com/images/classifieds/ad_images/IMG_20130728_132750.jpg}" Height="73" Width="73" VerticalAlignment="Top" Margin="0,10,8,0"/> -->
                    <StackPanel Width="370">
                        <TextBlock Text="{Binding title}" Foreground="#FFC8AB14" FontSize="28" />
                        <TextBlock Text="{Binding city}" TextWrapping="Wrap" FontSize="24" />
<TextBlock Text="{Binding realdata}" TextWrapping="Wrap" FontSize="24" />
 <TextBlock Text="{Binding gender}" TextWrapping="Wrap" FontSize="24" />
 <TextBlock Text="{Binding age}" TextWrapping="Wrap" FontSize="24" />
                        <TextBlock Text="{Binding price}" TextWrapping="Wrap" FontSize="24" />
                    </StackPanel>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

这工作正常。

但我在文本块中有一个条件。

if ( realdata == 1)
gender and age should be display and price should be hide.
else
price should be display. and Gender with age should be hide.

请帮我。

4

2 回答 2

2

您可以通过 Converter 使用绑定在 realdata 上的 Visibility 属性。因此,您的 .xaml 文件应该与以下代码片段中的外观一样:

<ListBox Height="600" HorizontalAlignment="Left" Margin="5,91,0,0" Name="productlist" VerticalAlignment="Top" Width="441" 
             SelectionChanged="productlistselectionchanged">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal" Height="132">
                    <!--    <Image Source="{Binding Path=http://callme4.com/images/classifieds/ad_images/IMG_20130728_132750.jpg}" Height="73" Width="73" VerticalAlignment="Top" Margin="0,10,8,0"/> -->
                    <StackPanel Width="370">
                        <TextBlock Text="{Binding title}" Foreground="#FFC8AB14" FontSize="28" />
                        <TextBlock Text="{Binding city}" TextWrapping="Wrap" FontSize="24" />
<TextBlock Text="{Binding realdata}" TextWrapping="Wrap" FontSize="24" />
 <TextBlock Text="{Binding gender}" Visibility="{Binding readldata, Converter={StaticResource VisibilityConverter}}" TextWrapping="Wrap" FontSize="24" />
 <TextBlock Text="{Binding age}" Visibility="{Binding readldata, Converter={StaticResource VisibilityConverter}}" TextWrapping="Wrap" FontSize="24" />
                        <TextBlock Text="{Binding price}" Visibility="{Binding readldata, Converter={StaticResource VisibilityConverter}}" TextWrapping="Wrap" FontSize="24" />
                    </StackPanel>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

在这种情况下,您必须创建 VisibilityConverter 类:

public sealed class VisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            int convertValue = (int)value;
            if (convertValue == 1)
                return Visibility.Collapsed;
            else
                return Visibility.Visible;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return null;
        }
    }

并将此类添加到 app.xaml 中的应用程序资源中。首先将转换器类的命名空间添加到 app.xaml:

xmlns:converters="clr-namespace:MyApplicationNameSpace"

然后将以下代码行添加到您的资源中:

<converters:VisibilityConverter  x:Key="VisibilityConverter" />

当您使用 MVVM 模式或只是喜欢使用数据绑定时,此解决方案不是那么短,但它很有帮助。

于 2013-09-05T10:00:43.303 回答
0

拿出你清单上的每一项并给出条件。

试试这个..

foreach (Product currentProduct in rootObject ) // Loop through List with foreach
{
        if(Product.realdata == 1) Price = "";
            else {Gender =""; Age="";}
}

productlist.ItemsSource = rootObject;

于 2013-09-04T04:14:24.607 回答