My Xaml Code :
<ListBox Name="lstCheckNotPass" ScrollViewer.HorizontalScrollBarVisibility="Disabled" Visibility="Collapsed" BorderThickness="0" Background="Transparent" ItemsSource="{Binding ElementName=dataGridNotPassCheck,Path=ItemsSource}">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Height="80" Width="{Binding ElementName=panelCheck,Path=Width,Converter={StaticResource Width30Percent}}" MinWidth="150" Margin="0,2" >
<Border Background="{Binding Type,Converter={StaticResource CheckBackgroundConverter}}" BorderThickness="1" BorderBrush="Gray" CornerRadius="5" Opacity="0.5"/>
<Grid Margin="5">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Label Content="{Binding CheckPersianDate}" Style="{x:Null}" Grid.Column="0" VerticalAlignment="Center" HorizontalContentAlignment="Left" FontWeight="Bold" FontSize="12"/>
<Label Content="{Binding Price,Converter={StaticResource DecimalToStringConverter}}" Style="{x:Null}" Grid.Column="1" VerticalAlignment="Center" HorizontalContentAlignment="Right" FontWeight="Bold" FontSize="12"/>
<Label Content="{Binding CustomerName}" Style="{x:Null}" Grid.Row="1" Grid.ColumnSpan="2" VerticalAlignment="Center" HorizontalContentAlignment="Center" FontSize="12" />
<Label Content="{Binding CheckId}" Style="{x:Null}" Grid.Row="2" Grid.ColumnSpan="2" VerticalAlignment="Center" HorizontalContentAlignment="Center" FontSize="12" />
</Grid>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Converter : PaymentType.PayMoney is string type for database text is 'PayMoney'
public class CheckBackground : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
try
{
if (value == null || value == PaymentTypes.PayMoney)
return Brushes.Orange;
else
return
Brushes.Green;
}
catch
{
return Brushes.Green;
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return (value == null || ((Brush)value) == Brushes.Green) ? PaymentTypes.EarnMoney : PaymentTypes.PayMoney;
}
}
I use breakpoint in try block it return color correctly Green or Orange. But background of list's items not change.
Binding Code:
var varChecks = PaymentQuery.GetNotPassChecks(true);
if (varChecks.Count>0)
lstCheckNotPass.ItemsSource = new ObservableCollection<Payment>(varChecks);
Payment Class :
public class Payment : NotifyPropertyChanged
{
private long _paymentId;
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public long PaymentId
{
get { return _paymentId; }
set
{
_paymentId = value;
OnPropertyChanged(nameof(PaymentId));
}
}
private string _date;
public string CheckPersianDate
{
get { return _date; }
set
{
_date = value;
OnPropertyChanged(nameof(PersianDate));
}
}
private decimal _price;
public decimal Price
{
get { return _price; }
set
{
_price = value;
OnPropertyChanged(nameof(Price));
}
}
private string _type;//Contain PayMoney Or EarnMoney text
public string Type
{
get { return _type; }
set
{
_type = value;
OnPropertyChanged(nameof(Type));
}
}
private string _customerName;
public string CustomerName;
{
get { return _customerName; }
set
{
_type = customerName;
OnPropertyChanged(nameof(CustomerName));
}
}
private long? _checkId;
public long? CheckId
{
get { return _checkId; }
set
{
_checkId = value;
OnPropertyChanged(nameof(CheckId));
}
}
}
result