简体   繁体   English

在 WPF 中将颜色绑定到背景

[英]Bind color to background in WPF

I'm trying to bind color to my UserControl background from double property modified by converter.我正在尝试将颜色从由转换器修改的双属性绑定到我的UserControl背景。 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.有一个按钮可以触发函数,该函数在单击时从文本框中设置PaceLabel.Speed属性。 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自己的组件.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自己的组件.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!提前致谢!

You need your PaceLabel class to implement interface: INotifyPropertyChanged,你需要你的 PaceLabel 类来实现接口:INotifyPropertyChanged,

Or rewrite its properties as DependencyProperties.或者将其属性重写为 DependencyProperties。

PaceLabel应该实现INotifyPropertyChanged并且PaceLabel.Speed应该触发该事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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