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 Name="price" Text="{Binding price}" TextWrapping="Wrap" FontSize="24" />
                        </StackPanel>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

我的列表框工作正常。

但是现在我在价格文本块中有一个条件,例如:-

if(price > 2000)
 textblock values should be purchased.
else
 textblock values should be "not purchased"

但我对此感到困惑,我该怎么办?

请专家检查

4

2 回答 2

0

使用价值转换器。在这里查看一些示例

创建一个转换器类

public class PurchaseStatusConverter : IValueConverter
{
    private const double comparePrice = 2000;

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        double price;

        if (double.TryParse(value.ToString(), out price))
        {
            return price > comparePrice ? "Purchased" : "Not Purchased";
        }

        return "-";
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

在您的 app.xaml 中,定义转换器,处理命名空间,如果您安装了 re-sharper,它应该会为您处理。

   <Application.Resources >
    <ResourceDictionary>
        <converter:PurchaseStatusConverter xmlns:converter="clr-namespace:namespacetoyourtype" x:Key="PurchaseStatusConverter" />
    <ResourceDictionary>
</Application.Resources >

最后,在您的文本框中,引用转换器。

<TextBlock Name="price" Text="{Binding price, Converter={StaticResource PurchaseStatusConverter}}" TextWrapping="Wrap" FontSize="24" />
于 2013-09-03T11:23:18.083 回答
0
/// <summary>
/// Returns a collapsed visibility when the price is less than or equal to the comparison price. 
/// </summary>
public class PriceToVisibilityConverter : IValueConverter
{
    private const double comparePrice = 2000;

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        double price;

        if (double.TryParse(value.ToString(), out price))
        {
            return price > comparePrice ? Visibility.Visible : Visibility.Collapsed;
        }

        return Visibility.Collapsed;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

在你的 app.xaml

<Application.Resources >
    <ResourceDictionary>
        <converter:PurchaseStatusConverter xmlns:converter="clr-namespace:namespacetoyourtype" x:Key="PurchaseStatusConverter" />
        <converter:PriceToVisibilityConverter xmlns:converter="clr-namespace:namespacetoyourtype" x:Key="PriceToVisibilityConverter" />
    <ResourceDictionary>
</Application.Resources >

在你的xml中

<TextBlock Name="price" Vis Text="Some text" Visibility="{Binding Price, Converter={StaticResource PriceToVisibilityConverter}}" TextWrapping="Wrap" FontSize="24" />
于 2013-09-03T11:57:43.363 回答