2017-10-01 8 views
-2

コンバーターによって変更された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としてそのプロパティを書き換える:

答えて

0

あなたがインターフェイスを実装するためにあなたのPaceLabelクラスが必要です。

0

PaceLabelは、INotifyPropertyChangedPaceLabel.Speedを実装して、そのイベントをトリガーする必要があります。