简体   繁体   中英

WPF binding a background color does not updated

I work on Appointments system, I have a Listbox that contain DataTemplate has a toggle button represent each appointment, If the appointment confirmed the background color should change, This is my ListBox :

<ListBox>                       
        <ListBox.ItemTemplate>
            <DataTemplate>
                <ToggleButton Background="{Binding Converter={StaticResource AppointmentStatusColor}, NotifyOnValidationError=True, UpdateSourceTrigger=PropertyChanged}" IsChecked="{Binding IsSelected, RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}, Mode=FindAncestor}}">
                    <StackPanel Orientation="Horizontal" Width="370">
                        <TextBlock Text="{Binding AppointmentID}"/>
                        <TextBlock Text="{Binding Patient.FullName}"/>
                    </StackPanel>
                </ToggleButton>
            </DataTemplate>
        </ListBox.ItemTemplate>
</ListBox>

The Background property is binding to IValueConverter:

public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        Brush _appointmentStatusBrush = null;
        var fc = new BrushConverter();
        if (value == null)
            return null;
        Appointment _appointment = (Appointment)value;
        switch (_appointment.Status)
        {
            case 0:
                _appointmentStatusBrush = (Brush)fc.ConvertFrom("#FFFBF7CC");
                break;
            case 1:
                _appointmentStatusBrush = (Brush)fc.ConvertFrom("#FFFFFFFF");
                break;
        }
        return _appointmentStatusBrush;
    }

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

The binding works when adding a new appointment, but when I update the existing appointment status and refresh the list, the color does not updated !!

Thanks in advance

Background property should be binded to Appointment.Status property with Mode=TwoWay .

Then, when Status changes, Converter will be invoked.

So, your binding should look like : Background="{Binding Status, Converter={StaticResource AppointmentStatusColor}, NotifyOnValidationError=True, UpdateSourceTrigger=PropertyChanged}"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM