-2

I'm trying to bind color to my UserControl background from double property modified by converter. However because of some reason it isn't work. It never breaks if I had a breakpoint in my convert function.

There is button that fires the function which sets the PaceLabel.Speed property from textbox on click. That part is working correctly so that I didn't copy paste that part of code here.

Here's a part of my code:

OwnComponent.xaml

<UserControl x:Class="OwnComponentNs.OwnComponent"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:OwnComponentNs"
         mc:Ignorable="d" Width="Auto">
...
<UserControl.Resources>
    <local:DoubleToSolidColorBrushConverter x:Key="doubleToBackgroundConverter" />
</UserControl.Resources>
<UserControl.Background>
    <Binding ElementName="paceLabel" Path="Speed" Converter="{StaticResource doubleToBackgroundConverter}" />
</UserControl.Background>

<local:PaceLabel x:Name="paceLabel" />
...

OwnComponent.xaml.cs

namespace OwnComponentNs
{
    public partial class OwnComponent : UserControl
    {
        public OwnComponent()
        {
            InitializeComponent();
        }

    }

    public class DoubleToSolidColorBrushConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            byte val = System.Convert.ToByte((double)value);
            return new SolidColorBrush(Color.FromRgb(val, val, val));
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }
    }

    public class PaceLabel : Label
    {
        private double _duration = 0;
        private double _distance = 0;
        private double _speed = 0;

        public double Duration
        {
            get { return _duration; }
            set { _duration = value; UpdateText(); }
        }

        public double Distance
        {
            get { return _distance; }
            set { _distance = value; UpdateText(); }
        }

        public double Speed
        {
            get { return _speed; }
            set { _speed = value; }
        }

        public PaceLabel()
        {
            UpdateText();
        }

        private void UpdateText()
        {
            double pace = Distance == 0 ? 0 : TimeSpan.FromHours(Duration).TotalMinutes / Distance;
            Content = Math.Round(pace, 2) + " min/km";
        }

    }
}

Please let me know if you need more details. Thanks in advance!

lingo
  • 1,848
  • 6
  • 28
  • 56

2 Answers2

0

You need your PaceLabel class to implement interface: INotifyPropertyChanged,

Or rewrite its properties as DependencyProperties.

Mishka
  • 508
  • 3
  • 5
0

PaceLabel should implement INotifyPropertyChanged and PaceLabel.Speed should trigger that event.

Kostya
  • 849
  • 5
  • 14