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

1

I have a ListBox that loads dynamically using EF. One of the filters used is that the product exists in the 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;

XAML code from 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 the ListBox appears to the user ...

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

    
asked by anonymous 15.04.2014 / 15:02

1 answer

1

You need to set a ValueConverter that converts its quantity (float) to bool so that it can be used with the IsEnabled property (bool).

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 Convert method of the above class will return false when the amount is zero.

Use Convert by adding a ItemContainerStyle to your ListBox :

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

Do not forget to include a reference to Converter :

<converters:QuantidadeToBoolConverter x:Key="QuantidadeToBool"/>
    
15.04.2014 / 16:06