변환기로 수정 한 double 속성에서 내 UserControl
배경에 색을 바인딩하려고합니다. 그러나 어떤 이유로 그것은 작동하지 않습니다. 변환 기능에 중단 점이 있으면 절대로 깨지지 않습니다.WPF에서 배경에 색을 바인딩
클릭시 텍스트 상자에서 PaceLabel.Speed
속성을 설정하는 기능을 실행하는 버튼이 있습니다. 그 부분은 제대로 작동하기 때문에 코드의 일부분을 여기에 복사하지 않았습니다. 여기
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";
}
}
}
당신이 세부 사항을 더 필요로하는 경우 알려 주시기 바랍니다 . 미리 감사드립니다! ,
을에서 INotifyPropertyChanged 또는 DependencyProperties로 속성을 재 작성 :