How do I leave specific items in my dynamic listbox disabled?

Asked

Viewed 299 times

1

I have a Listbox that is dynamically loaded using EF. One of the filters used is that the product exists in stock.

var query = (from p in db.Products
                        join s in db.Stocks
                         on new { p.SchoolID, p.ProductID }
                        equals new { s.SchoolID, s.ProductID }
                        where
                            s.Quantity > 0 &&
                            p.Active == true &&
                            p.SchoolID == _idSchool &&
                            p.ProductTypeID == 1
                        select p);
        lst.ItemsSource = query;

Code XAML of my Listbox...

<ListBox.ItemTemplate>
                    <DataTemplate>
                        <Grid>
                            <Border>
                                <StackPanel Margin="1">
                                    <StackPanel.Background>
                                        <ImageBrush ImageSource="images/btnSalgados.png"/>
                                    </StackPanel.Background>
                                    <TextBlock Margin="0,0,0,0" Padding="4" Width="167" Height="52" TextWrapping="Wrap" Text="{Binding Name}" ToolTip="{Binding Name}" 
                                        TextAlignment="Center" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="22" Foreground="White" />
                                    <TextBlock Margin="0,0,0,0"  Padding="4" Width="167" Height="40" TextWrapping="Wrap" Text="{Binding Price, StringFormat=\{0:N\}}" 
                                        TextAlignment="Center" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="22" Foreground="White"/>
                                </StackPanel>
                            </Border>
                        </Grid>
                    </DataTemplate>
                </ListBox.ItemTemplate>

The image of how Listbox appears for the user...

A imagem de como aparece o ListBox para o usuário...

I need that if the user selects a product that only has 1 in the stock (because when selecting it the stock of it will be zeroed) this item will be disabled for future selections (so that it does not select a product that does not have in stock).

1 answer

1

You need to define a ValueConverter convert its quantity(float) to bool so that it can be used with the Isenabled(bool property).

class QuantidadeToBoolConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var quant = (float) value;
        return quant != 0;
    }

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

The method Convert of the above class will return false when the quantity is zero.

Use Convert by adding one ItemContainerStyleto his ListBox:

<ItemContainerStyle>
    <Style TargetType="{x:Type ListBoxItem}">
        <Setter Property="IsEnabled" Value="{Binding quantidade, Converter={StaticResource QuantidadeToBool}}"/>
    </Style>
</ItemContainerStyle>

Be sure to include a reference to Converter:

<converters:QuantidadeToBoolConverter x:Key="QuantidadeToBool"/>
  • I don’t know if it doesn’t work because it’s a dynamic list or I have something really wrong. In Setter Property if I put the Active field that exists in the Products table it gets Enable = false but when I try to use this conversion class it does not get Disabled.

  • I got it... Listboxitem item = (Listboxitem)lst.ItemContainerGenerator.Containerfromindex(lst.Selectedindex); item.Isenabled = false;

  • As I do not have access to your code little more I can add unless the code I posted works, because I tested it and it is something I use frequently. Of course the DataContext of ListBox must have a property called quantidade that receives the value Quantity that comes from the comic book, just like you have to Nameand Price

  • Yes the XAML part worked when I put a Field that comes from my Active BD. But when I tried to use the Quantity it accessed the Quantityboolconverter class...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.