【问题标题】:ListBox items changes based on conditionListBox 项目根据条件更改
【发布时间】:2013-09-03 11:03:00
【问题描述】:

我以 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>

我的列表框工作正常。

但现在我在 price textblock 中有一个条件,例如:-

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

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

请专家检查

【问题讨论】:

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


    【解决方案1】:

    使用值转换器。查看一些示例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" />
    

    【讨论】:

    • 如果我想根据一个条件隐藏一个文本块,如果条件为真,那么两个新的文本块应该显示另一个。
    • 您仍然需要使用值转换器并将可见性设置为可见或折叠。然后,您可以将结果绑定到控件的可见性属性。查看编辑后的答案
    【解决方案2】:
    /// <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" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-24
      • 2011-07-14
      • 1970-01-01
      • 1970-01-01
      • 2018-06-07
      • 1970-01-01
      • 2019-05-18
      • 1970-01-01
      相关资源
      最近更新 更多