1

I'm trying to change the background of a button, if a property of the data binding is true.

To give a short overview - I have a ListBox. I also gave this ListBox a style for the items:

  <ListBox Margin="0,5,0,0" Background="Transparent" 
             BorderThickness="0" 
             ItemsSource="{Binding PaymentsAndCollectionData.PaymentsToCreditors}"
             SelectedItem="{Binding PaymentsAndCollectionData.SelectedPaymentToCreditor}">
        <ListBox.ItemContainerStyle>
            .......
        </ListBox.ItemContainerStyle>
  </ListBox

Now, I want to put a button in this style. So, I used a ControlTemplate like so (I know this looks weird, and I could propably have done this differently!):

<Style TargetType="ListBoxItem">
<Setter Property="Template">
    <Setter.Value>
        <ControlTemplate TargetType="ListBoxItem">
            <Button>
                <Button.Style>
                    <Style TargetType="Button">
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate>
                                    <Button Name="Button" HorizontalContentAlignment="Stretch" Background="Transparent" BorderThickness="0"
                                            Command="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, 
                                                      Path=DataContext.PaymentsAndCollectionData.SelectPaymentToCreditorCommand}" 
                                            CommandParameter="{Binding}">
                                        <DockPanel Name="DP" LastChildFill="False" Background="{TemplateBinding Background}">
                                            <TextBlock DockPanel.Dock="Left" Text="{Binding Name}" Margin="20,0,10,0" Padding="0,5,0,5" Foreground="{TemplateBinding Foreground}"/>
                                            <StackPanel Orientation="Horizontal" DockPanel.Dock="Right">
                                                <TextBlock Text="R" VerticalAlignment="Center" Foreground="{TemplateBinding Foreground}"/>
                                                <TextBlock Text="{Binding Amount, StringFormat=N2}" VerticalAlignment="Center" Margin="0,0,10,0" Foreground="{TemplateBinding Foreground}"/>
                                            </StackPanel>
                                        </DockPanel>
                                    </Button>
                                    <ControlTemplate.Triggers>
                                        <DataTrigger Binding="{Binding IsSelected}" Value="True">
                                            <Setter TargetName="DP" Property="Background" Value="{StaticResource APPLICATION_GREEN_COLOR}" />
                                            <Setter Property="Foreground" Value="White" />
                                        </DataTrigger>
                                    </ControlTemplate.Triggers>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </Button.Style>
            </Button>
        </ControlTemplate>
    </Setter.Value>
</Setter>
</Style>

So, the above works properly. The Name and Amount is set from the DataBinding. I am just failing to set the background, if IsSelected is true.

My binding object looks like this:

public class PaymentItem
{
    public string Name { get; set; }

    public decimal Amount { get; set; }

    public bool IsSelected { get; set; }
}
frogatto
  • 28,539
  • 11
  • 83
  • 129
Fred
  • 2,402
  • 4
  • 31
  • 58
  • Possible duplicate of [WPF DataBinding not updating?](https://stackoverflow.com/questions/14965796/wpf-databinding-not-updating) – tabby Aug 02 '17 at 13:54

1 Answers1

1

If you change the IsSelected property after the object is created, you need to implement INotifyPropertyChanged interface in this object to make the Binding work.

public class PaymentItem : INotifyPropertyChanged
{
    private bool _isSelected;

    public string Name { get; set; }

    public decimal Amount { get; set; }

    public bool IsSelected
    {
        get
        {
            return _isSelected;
        }
        set
        {
            _isSelected = value;
            OnPropertyChanged();
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    [NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}
Yevgeniy
  • 1,054
  • 1
  • 9
  • 17