ListBox项目根据条件而变化

时间:2013-09-03 11:03:00

标签: json windows-phone-7 windows-phone-8 windows-phone

我在JSON中获取数据并通过

存储在List中
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"

但我很困惑,我该怎么办?

请专家检查

2 个答案:

答案 0 :(得分:0)

使用值转换器。看一些样本here

创建转换器类

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" />

答案 1 :(得分: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 >

在你的xaml

<TextBlock Name="price" Vis Text="Some text" Visibility="{Binding Price, Converter={StaticResource PriceToVisibilityConverter}}" TextWrapping="Wrap" FontSize="24" />