WPF binding background not update

hossein tavakoli 471 Reputation points
2024-08-08T14:11:29.9066667+00:00

Hi dears, I wanna set background color with binding and converter!

Converter Code :


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

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return (value == null || ((Brush)value) == Brushes.Green) ? PaymentTypes.EarnMoney : PaymentTypes.PayMoney;
        }
    }

WPF Xaml :

<ListBox ScrollViewer.HorizontalScrollBarVisibility="Disabled" Background="Transparent" Name="lstCheckNotPass" ItemsSource="{Binding ElementName=dataGridNotPassCheck,Path=ItemsSource}">
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel Orientation="Horizontal"/>
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid Height="80" Width="199" Margin="0,2" >
                <Border Background="{Binding Type,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged,Converter={StaticResource CheckBackgroundConverter}}" Height="80" Width="195" BorderThickness="1" BorderBrush="Gray" CornerRadius="5" Opacity="0.9"/>
                <Grid Margin="5">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition/>
                        <ColumnDefinition/>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition/>
                        <RowDefinition/>
                        <RowDefinition/>
                    </Grid.RowDefinitions>
                    <Label Content="{Binding Price,Converter={StaticResource DecimalToStringConverter}}" Style="{x:Null}" Grid.Column="0" VerticalAlignment="Center" HorizontalContentAlignment="Right" FontWeight="Bold" FontSize="12"/>
                    <Label Content="{Binding CheckPersianDate}" 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>
                                    

I use breakpoint in CheckBackground converter , it return color correctly but Border Background not updated.

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,963 questions
{count} votes

2 answers

Sort by: Most helpful
  1. hossein tavakoli 471 Reputation points
    2024-08-10T08:07:42.2333333+00:00

    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.

    Screenshot 2024-08-10 113406

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

    resultScreenshot 2024-08-10 110808


  2. hossein tavakoli 471 Reputation points
    2024-08-18T08:18:49.0266667+00:00

    Solved. My converter return Brush (System.Drawing) , should return Brush as System.Windows.Media

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.