ListBox items TextBlock changes based on condition

384 Views Asked by At

I am getting data in JSON and storing in List by

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

and after that, I am displaying data in ListBox by

  productlist.ItemsSource = rootObject;

My xaml file:-

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

This is working fine.

But i have a condition in textblock.

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.

Please help me.

2

There are 2 best solutions below

0
On BEST ANSWER

Take each item on your list and give condition.

try this one ..

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

productlist.ItemsSource = rootObject;

0
On

You can use Visibility property bound on realdata with Converter. So your .xaml file should look as you can you on the following code snippet:

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

In this case you have to create VisibilityConverter class:

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;
        }
    }

And add this class to your application resources in app.xaml. First of all add namespace of your converter class to app.xaml:

xmlns:converters="clr-namespace:MyApplicationNameSpace"

and then add following line of code into your resources:

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

This solution not so short, but it helpful, when you use MVVM-pattern or just like to use data binding.