Home > Software design >  Multibindingconverter not changing color in C# WPF app
Multibindingconverter not changing color in C# WPF app

Time:04-11

In my WPF Application I try to change the color of one datagrid cell based on the value of two other cells in that row.

This is an excerpt from my XML:

 <DataGridTextColumn Header="Element1" Binding="{Binding Element1}">
                            <DataGridTextColumn.ElementStyle>
                                <Style TargetType="{x:Type TextBlock}">
                                    <Setter Property="Background">
                                        <Setter.Value>
                                        <MultiBinding Converter="{StaticResource MultiColorConverter}">
                                                <MultiBinding.Bindings>
                                                    <Binding Path="StartDate" />
                                                    <Binding Path="EndDate" />
                                                </MultiBinding.Bindings>
                                                
                                        </MultiBinding>
                                        </Setter.Value>
                                    </Setter>
                                </Style>
                            </DataGridTextColumn.ElementStyle>
                        </DataGridTextColumn>

Here is my Converter:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;

namespace LiveLogosStatus.Converter
{
    class MultiColorConverter : IMultiValueConverter
    {

        public MultiColorConverter()
        {
        }

        public object Convert(object[] value, Type targetType, object parameter, CultureInfo culture)
        {
           if(value[1] == null || value[0] == null)
            {
                return Brushes.Blue;
            }
                DateTime startDate = (DateTime)value[0];
                DateTime endDate = (DateTime)value[1];
                if (DateTime.Now >= startDate && DateTime.Now < endDate)
                {
                return Brushes.Yellow;
                }
                else
                {
                return Brushes.Red;
                }
            
        }

        public object[] ConvertBack(object value, Type[] targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

I checked while debugging and my Convert Method fires and returns the correct values. However the color doesn't change in the final view.

Edit: Even when changing from SolidBrush to the static Brushes type, it still doesn't work. I had Brushes.Red etc. already and only tried SolidBrush when that didn't work.

CodePudding user response:

Ok, thanks @Clemens for bringing me on the right track

For some reason my Visual Studio decided that using System.Drawing; was the correct using statement. Unfortunately System.Drawing.Brushes exists and seems to also return a SolidBrush.

After adding using System.Windows.Media; and deleting using System.Drawing; everything works as expected. Especially if SolidBrush is a part of WinForms I don't understand why VS would even tell me to use System.Drawing.

  • Related