Json 列表框项会根据条件进行更改
我正在获取JSON格式的数据,并通过Json 列表框项会根据条件进行更改,json,windows-phone-7,windows-phone-8,windows-phone,Json,Windows Phone 7,Windows Phone 8,Windows Phone,我正在获取JSON格式的数据,并通过 List<Product> rootObject = JsonConvert.DeserializeObject<List<Product>>(e.Result); 我的xaml文件:- <ListBox Height="600" HorizontalAlignment="Left" Margin="5,91,0,0" Name="productlist" VerticalAlignment="Top" Widt
List<Product> rootObject = JsonConvert.DeserializeObject<List<Product>>(e.Result);
我的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>
但我对此感到困惑,我该怎么办
请专家检查使用值转换器。看到一些样品了吗 创建一个转换器类
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" />
//
///当价格小于或等于比较价格时,返回折叠的可见性。
///
公共类PriceToVisibilityConverter:IValueConverter
{
专用常量双比较价格=2000;
公共对象转换(对象值、类型targetType、对象参数、System.Globalization.CultureInfo区域性)
{
双倍价格;
if(double.TryParse(value.ToString(),out price))
{
返回价格>比较价格?可见性。可见:可见性。折叠;
}
返回可见性。折叠;
}
公共对象转换回(对象值、类型targetType、对象参数、System.Globalization.CultureInfo区域性)
{
抛出新的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" />
如果我想根据条件隐藏textblock,并且如果条件为true,则应显示两个新的textblock(另一个)。您仍然需要使用值转换器,并将可见性设置为Visible或Colapsed。然后,可以根据“控件可见性”属性绑定结果。见编辑后的答案
/// <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();
}
}
<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 >
<TextBlock Name="price" Vis Text="Some text" Visibility="{Binding Price, Converter={StaticResource PriceToVisibilityConverter}}" TextWrapping="Wrap" FontSize="24" />