C# 另一个WPF“;“绑定不更新”;问题

C# 另一个WPF“;“绑定不更新”;问题,c#,wpf,mvvm,C#,Wpf,Mvvm,这是我的自定义控件,它是一种简单的进度条: using System; using System.ComponentModel; using System.Globalization; using System.Windows; using System.Windows.Controls; using System.Windows.Media; namespace eg.BJM.Controls { public class HorizontalBarGauge : Canvas

这是我的自定义控件,它是一种简单的进度条:

using System;
using System.ComponentModel;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace eg.BJM.Controls
{
    public class HorizontalBarGauge : Canvas
    {
        public HorizontalBarGauge()
        {
            IsSigned = true;
            MinValue = -100;
            MaxValue = 100;
            Value = 25;
            MarkerValue = -1;
            BorderColor = Colors.Black;
            MarkerColor = Colors.Red;
            BarColor = Colors.SkyBlue;
            BaseColor = Colors.DodgerBlue;
            ValueColor = Colors.White;
        }

        #region Dependecy Properties

        public static readonly DependencyProperty ValueProperty =
        DependencyProperty.Register("Value", typeof(int), typeof(HorizontalBarGauge));

    public static readonly DependencyProperty MinValueProperty =
        DependencyProperty.Register("MinValue", typeof(int), typeof(HorizontalBarGauge));

    public static readonly DependencyProperty MaxValueProperty =
        DependencyProperty.Register("MaxValue", typeof(int), typeof(HorizontalBarGauge));

    public static readonly DependencyProperty MarkerValueProperty =
        DependencyProperty.Register("MarkerValue", typeof(int), typeof(HorizontalBarGauge));

    public static readonly DependencyProperty MarkerColorProperty =
        DependencyProperty.Register("MarkerColor", typeof(Color), typeof(HorizontalBarGauge));

    public static readonly DependencyProperty BaseColorProperty =
        DependencyProperty.Register("BaseColor", typeof(Color), typeof(HorizontalBarGauge));

    public static readonly DependencyProperty BarColorProperty =
        DependencyProperty.Register("BarColor", typeof(Color), typeof(HorizontalBarGauge));

    public static readonly DependencyProperty BorderColorProperty =
        DependencyProperty.Register("BorderColor", typeof(Color), typeof(HorizontalBarGauge));

    public static readonly DependencyProperty BorderThicknessProperty =
        DependencyProperty.Register("BorderThickness", typeof(double), typeof(HorizontalBarGauge));

    public static readonly DependencyProperty IsSignedProperty =
        DependencyProperty.Register("IsSigned", typeof(bool), typeof(HorizontalBarGauge));

    public static readonly DependencyProperty ShowValueProperty =
        DependencyProperty.Register("ShowValue", typeof(bool), typeof(HorizontalBarGauge));

    public static readonly DependencyProperty ValueColorProperty =
        DependencyProperty.Register("ValueColor", typeof(Color), typeof(HorizontalBarGauge));

    #endregion

    [Description("Determines whether the bar shows [min,max] or [-max,+max]")]
    public bool IsSigned
    {
        get { return (bool)GetValue( IsSignedProperty ); }    
        set
        {
            SetValue( IsSignedProperty, value );
            if (value)
            {
                MinValue = -MaxValue;
            }
            else
            {
                MinValue = 0;
            }
            reassertValues();
            InvalidateVisual();
        }
    }

    [Description("Get/set the current value to show.")]
    public int Value
    {
        get { return (int)GetValue(ValueProperty); }
        set
        {
            SetValue( ValueProperty, value );
            InvalidateVisual();
        }
    }

    [Description("Get/set the minimum value to show.")]
    public int MinValue
    {
        get { return (int)GetValue(MinValueProperty); }
        set
        {
            SetValue(MinValueProperty, value);
            reassertValues();
            InvalidateVisual();
        }
    }

    [Description("Get/set the maximum value to show.")]
    public int MaxValue
    {
        get { return (int)GetValue(MaxValueProperty); }
        set
        {
            value = Math.Max(value, 1);
            SetValue(MaxValueProperty, value);
            reassertValues();
            InvalidateVisual();
        }
    }

    [Description("Get/set the marker value to show. Negative => no marker.")]
    public int MarkerValue
    {
        get { return (int)GetValue(MarkerValueProperty); }
        set
        {
            SetValue(MaxValueProperty, value);
            reassertValues();
            InvalidateVisual();
        }
    }

    [Description("Get/set the color of the marker.")]
    public Color MarkerColor
    {
        get { return (Color)GetValue(MarkerColorProperty); }
        set
        {
            SetValue(MarkerColorProperty, value);
            Brush penBrush = new SolidColorBrush( value );
            _markerPen = new Pen( penBrush, 3 );
        }
    }

    [Description("Get/set the value text color.")]
    public Color ValueColor
    {
        get { return (Color)GetValue(ValueColorProperty); }
        set
        {
            SetValue(ValueColorProperty, value);
            _textBrush = new SolidColorBrush(value);
        }
    }


    [Description("Get/set the base color.")]
    public Color BaseColor
    {
        get { return (Color)GetValue(BaseColorProperty); }
        set
        {
            SetValue(BaseColorProperty, value);
            _bodyBrush = new SolidColorBrush( value );
        }
    }

    [Description("Get/set the bar color.")]
    public Color BarColor
    {
        get { return (Color)GetValue(BarColorProperty); }
        set
        {
            SetValue(BarColorProperty, value);
            _barBrush = new SolidColorBrush( value );
        }
    }

    [Description("Get/set the border color.")]
    public Color BorderColor
    {
        get { return (Color)GetValue(BorderColorProperty); }
        set
        {
            SetValue(BorderColorProperty, value);
            var brush = new SolidColorBrush( value );
            _borderPen = new Pen( brush, BorderThickness );
        }
    }

    [Description("Get/set the border thickness.")]
    public double BorderThickness
    {
        get { return (double)GetValue(BorderThicknessProperty); }
        set
        {
            SetValue(BorderThicknessProperty, value);
            var brush = new SolidColorBrush( BorderColor );
            _borderPen = new Pen( brush, value );
        }
    }

    [Description("Get/set whether values are displayed in the control.")]
    public bool ShowValue
    {
        get { return (bool)GetValue( ShowValueProperty ); }
        set { SetValue( ShowValueProperty, value );}
    }

    protected override void OnRender(DrawingContext drawingContext)
    {
        base.OnRender(drawingContext);

        // Background.
        drawingContext.DrawRectangle(_bodyBrush, null, _rectangle);

        // Bar.
        int value = Value;
        if (value != 0)
        {
            Rect barRect;
            if (IsSigned)
            {
                double frac = Convert.ToDouble(value)/Convert.ToDouble(MaxValue*2);
                if (value < 0)
                {
                    barRect = new Rect(_rectangle.Width/2-frac*_rectangle.Width, 0, _rectangle.Width/2, _rectangle.Height);
                }
                else
                {
                    barRect = new Rect( _rectangle.Width/2, 0, frac*_rectangle.Width, _rectangle.Height );
                }

                drawingContext.DrawRectangle( _barBrush, null, barRect );
            }
            else
            {
                double x = (_rectangle.Width*value)/(MaxValue - MinValue);
                if (x > 0)
                {
                    barRect = new Rect( 0, 0, x, _rectangle.Height );
                    drawingContext.DrawRectangle( _barBrush, null, barRect );
                }
            }
        }

        // Marker.
        if (MarkerValue >= 0)
        {
            if (IsSigned)
            {
                double frac = Convert.ToDouble( MarkerValue )/Convert.ToDouble( MaxValue );
                double markerX1 = 0.5*_rectangle.Width*(1 + frac);
                Point p0 = new Point( markerX1, 0 );
                Point p1 = new Point( markerX1, _rectangle.Height );
                drawingContext.DrawLine( _markerPen, p0, p1 );
                double markerX2 = 0.5*_rectangle.Width*(1 - frac);
                Point p2 = new Point( markerX2, 0 );
                Point p3 = new Point( markerX2, _rectangle.Height );
                drawingContext.DrawLine( _markerPen, p2, p3 );
            }
            else
            {
                double markerX = (_rectangle.Width*MarkerValue)/(MaxValue - MinValue);
                Point p0 = new Point( markerX, 0 );
                Point p1 = new Point( markerX, _rectangle.Height );
                drawingContext.DrawLine( _markerPen, p0, p1 );
            }
        }

        // Border.
        drawingContext.DrawRectangle(null, _borderPen, _rectangle);

        // Text.
        if (ShowValue)
        {
            FormattedText text = new FormattedText( value.ToString(), CultureInfo.CurrentCulture,
                FlowDirection.LeftToRight, s_typeface, 12, _textBrush );
            Point pos = new Point( (_rectangle.Width - text.Width)/2, (_rectangle.Height - text.Height)/2 );
            drawingContext.DrawText( text, pos );
        }
    }

    protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
    {
        base.OnRenderSizeChanged(sizeInfo);
        _rectangle = new Rect( sizeInfo.NewSize );
        InvalidateVisual();
    }

    private int clamp(int value)
    {
        return (value < MinValue ? MinValue : value > MaxValue ? MaxValue : value);
    }

    private void reassertValues()
    {
        // Make sure max and min are correct.
        int oldMin = MinValue;
        int newMin = oldMin;

        int oldMax = MaxValue;
        int newMax = oldMax;

        if (oldMin > oldMax)
        {
            newMin = oldMax;
            newMax = oldMin;
        }

        if (newMin != oldMin)
        {
            SetValue(MinValueProperty, newMin);
        }

        if (newMax != oldMax)
        {
            SetValue(MaxValueProperty, newMax);
        }

        // Make sure value is in [min,max].
        int oldValue = Value;
        int newValue = clamp(oldValue);
        if (newValue != oldValue)
        {
            Value = newValue;
        }

        // Make sure marker is correctly placed.
        int marker = MarkerValue;
        if (marker >= 0)
        {
            int newMarker = clamp(marker);
            if (newMarker != marker)
            {
                SetValue(MarkerValueProperty, newMarker);
            }
        }
    }

    private Pen _borderPen;
    private Pen _markerPen;
    private Brush _barBrush;
    private Brush _bodyBrush;
    private Rect _rectangle;
    private Brush _textBrush;

    private static readonly Typeface s_typeface = new Typeface( "Verdana" );
}
使用系统;
使用系统组件模型;
利用制度全球化;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Media;
名称空间,例如BJM.Controls
{
公共类水平驳船:画布
{
公共水平驳船()
{
IsSigned=true;
最小值=-100;
最大值=100;
数值=25;
MarkerValue=-1;
边框颜色=颜色。黑色;
MarkerColor=颜色。红色;
BarColor=Colors.SkyBlue;
BaseColor=Colors.DodgerBlue;
ValueColor=颜色。白色;
}
#区域相关属性
公共静态只读从属属性ValueProperty=
DependencyProperty.Register(“值”、typeof(int)、typeof(HorizontalBarGauge));
公共静态只读从属属性MinValueProperty=
DependencyProperty.Register(“MinValue”、typeof(int)、typeof(HorizontalBarGauge));
公共静态只读从属属性MaxValueProperty=
DependencyProperty.Register(“MaxValue”、typeof(int)、typeof(HorizontalBarGauge));
公共静态只读DependencyProperty MarkerValueProperty=
从属属性寄存器(“MarkerValue”、typeof(int)、typeof(horizontalbargue));
公共静态只读从属属性MarkerColorProperty=
从属属性寄存器(“MarkerColor”、typeof(Color)、typeof(Horizontalbargue));
公共静态只读DependencyProperty BaseColorProperty=
从属属性寄存器(“基色”、类型(颜色)、类型(水平条纹));
公共静态只读DependencyProperty BarColorProperty=
从属属性寄存器(“BarColor”、typeof(Color)、typeof(Horizontalbargue));
公共静态只读DependencyProperty BorderColorProperty=
从属属性寄存器(“BorderColor”、typeof(Color)、typeof(horizontalbargue));
公共静态只读从属属性BorderThicknessProperty=
从属属性寄存器(“边界厚度”、类型(双精度)、类型(水平条形图));
公共静态只读从属属性IsSignedProperty=
从属财产登记簿(“IsSigned”,typeof(bool),typeof(horizontalbargue));
公共静态只读从属属性ShowValueProperty=
从属属性寄存器(“ShowValue”、typeof(bool)、typeof(horizontalbargue));
公共静态只读从属属性ValueColorProperty=
从属属性寄存器(“ValueColor”、typeof(Color)、typeof(Horizontalbargue));
#端区
[说明(“确定条形图显示的是[min,max]还是[-max,+max]”)
公共场所被禁止
{
获取{return(bool)GetValue(IsSignedProperty);}
设置
{
设置值(IsSignedProperty,value);
如果(值)
{
MinValue=-MaxValue;
}
其他的
{
最小值=0;
}
重新赋值();
无效的(通常的);
}
}
[说明(“获取/设置要显示的当前值”。)]
公共整数值
{
获取{return(int)GetValue(ValueProperty);}
设置
{
设置值(ValueProperty,value);
无效的(通常的);
}
}
[说明(“获取/设置要显示的最小值”。)]
公共整数最小值
{
获取{return(int)GetValue(MinValueProperty);}
设置
{
SetValue(MinValueProperty,value);
重新赋值();
无效的(通常的);
}
}
[说明(“获取/设置要显示的最大值”。)]
公共整数最大值
{
获取{return(int)GetValue(MaxValueProperty);}
设置
{
值=数学最大值(值,1);
SetValue(MaxValueProperty,值);
重新赋值();
无效的(通常的);
}
}
[说明(“获取/设置要显示的标记值。负值=>无标记。”)]
公共整数标记值
{
获取{return(int)GetValue(MarkerValueProperty);}
设置
{
SetValue(MaxValueProperty,值);
重新赋值();
无效的(通常的);
}
}
[说明(“获取/设置标记的颜色”)]
公共颜色标记颜色
{
获取{return(Color)GetValue(MarkerColorProperty);}
设置
{
设置值(标记颜色属性,值);
画笔画笔=新的SolidColorBrush(值);
_markerPen=新笔(画笔,3);
}
}
[说明(“获取/设置值文本颜色”)]
公共颜色价值
{
获取{return(Color)GetValue(ValueColorProperty);}
设置
{
SetValue(ValueColorProperty,值);
_textBrush=新的SolidColorBrush(值);
}
}
[说明(“获取/设置基础颜色”)]
公共色基色
{
获取{return(Color)GetValue(BaseColorProperty);}
设置
{
SetValue(BaseColorProperty,value);
_bodyBrush=新的SolidColorBrush(值);
}
}
[说明(“获取/设置条形图颜色”)]
公共颜色
{
获取{return(Color)GetValue(BarColorProperty);}
设置
{
SetValue(BarColorProperty,value);
_barBrush=新的SolidColorBrush(值);
}
}
[说明(“获取/设置边框颜色”)]
公共颜色边框颜色
{
获取{return(Color)GetValue(BorderColorProperty);}
设置
{
SetValue(BorderColorProperty,value);
var笔刷=新的SolidColorBrush(值);
_borderPen=新笔(画笔、边框厚度);
}
}
[说明(“获取/设置边框厚度”)]
公共双边界
        <controls:HorizontalBarGauge Grid.Row="1" Grid.Column="1"
                                 Margin="8" Width="130" Height="18"
                                 IsSigned="False"
                                 MinValue="0"
                                 MaxValue="100"
                                 BorderThickness="2"
                                 BorderColor="MidnightBlue"
                                 ShowValue="True"
                                 Value="{Binding StepperPosition}"
                                 />