【问题标题】:UserControl DataBinding not working用户控件数据绑定不起作用
【发布时间】:2014-12-07 21:49:23
【问题描述】:

大家好,我又需要你们的帮助了。

我的 XAML:

我想在 XAML 中将 Line1 的颜色绑定到我的 TestClassItem。

<Canvas Name="ElementCanvas">
    <local:TestClass x:Name="TestClassItem" Width="50" Height="20" Position="20,100" LineColor="{Binding Stroke, ElementName=Line1}" ></local:TestClass>
    <Line x:Name="Line1" X1="100" X2="300" Y1="50" Y2="50" Stroke="Red"/>
    <Line x:Name="Line2" X1="100" X2="300" Y1="100" Y2="100" Stroke="{Binding Stroke, ElementName=Line1}" />
</Canvas>

我的创建类非常简单:

public class TestClass : Canvas
{
    Line myLine = new Line();
    public Color LineColor
    {
        get
        {
            return (Color)GetValue(LineStrokeProperty);
        }
        set
        {
            SetValue(LineStrokeProperty, value);
            OnPropertyChanged("LineColor");
        }
    }

    double X
    {
        get
        {
            return (double)Canvas.GetLeft(this);
        }
        set
        {
            Canvas.SetLeft(this, value);
            OnPropertyChanged("X");
        }
    }

    double Y
    {
        get
        {
            return (double)Canvas.GetTop(this) + Height / 2;
        }
        set
        {
            Canvas.SetTop(this, value - Height / 2);
            OnPropertyChanged("Y");
        }
    }

    public Point Position
    {
        get
        {
            return new Point(X, Y);
        }
        set
        {
            X = value.X;
            Y = value.Y;
        }
    }

    public static readonly DependencyProperty LineStrokeProperty = DependencyProperty.Register("LineColor", typeof(Color), typeof(TestClass), new FrameworkPropertyMetadata(Colors.Purple, FrameworkPropertyMetadataOptions.AffectsMeasure, new PropertyChangedCallback(OnLineStrokePropertyChanged), new CoerceValueCallback(OnLineStrokePropertyCoerceValue)), new ValidateValueCallback(OnLineStrokePropertyValidateValue));

    private static bool OnLineStrokePropertyValidateValue(object value)
    {
        return true;
    }

    private static object OnLineStrokePropertyCoerceValue(DependencyObject d, object baseValue)
    {
        return (Color)baseValue;
    }

    private static void OnLineStrokePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        ((TestClass)d).SetValue(LineStrokeProperty, (Color)e.NewValue);

    }




    public TestClass()
    {
        DataContext = this;
        Background = Brushes.Yellow;
        SnapsToDevicePixels = true;
        Binding b_width = new Binding("Width");
        b_width.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
        myLine.SetBinding(Line.X2Property, b_width);

        Binding b_y1 = new Binding("Height");
        b_y1.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
        b_y1.Converter = new DoubleTwicer(); //Divides Height by 2
        myLine.SetBinding(Line.Y1Property, b_y1);

        Binding b_y2 = new Binding("Height");
        b_y2.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
        b_y2.Converter = new DoubleTwicer();
        myLine.SetBinding(Line.Y2Property, b_y2);

        Binding b_stroke = new Binding("LineColor");
        b_stroke.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
        b_stroke.Converter = new RaColorToSolidBrush();
        myLine.SetBinding(Line.StrokeProperty, b_stroke);

        this.Children.Add(myLine);
    }

    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

}

为什么它不起作用?

有人知道如何调试(请参阅 Visual Studio 中绑定的值?)

非常感谢你!!!!

【问题讨论】:

标签: c# wpf xaml binding dependency-properties


【解决方案1】:

首先!谢谢你们已经帮助了我,但它有效但不完全(DataContext = this; 可能是问题所以离开它:

 public Brush LineColor
        {
            get
            {
                return (Brush)GetValue(LineStrokeProperty);
            }
            set
            {
                SetValue(LineStrokeProperty, value);
                OnPropertyChanged("LineColor");
            }
        }



        public static readonly DependencyProperty LineStrokeProperty = DependencyProperty.Register("LineColor", typeof(Brush), typeof(RaClickableLine), new FrameworkPropertyMetadata(Brushes.White, FrameworkPropertyMetadataOptions.AffectsMeasure, new PropertyChangedCallback(OnLineStrokePropertyChanged), new CoerceValueCallback(OnLineStrokePropertyCoerceValue)), new ValidateValueCallback(OnLineStrokePropertyValidateValue));

        private static bool OnLineStrokePropertyValidateValue(object value)
        {
            return true;
        }

        private static object OnLineStrokePropertyCoerceValue(DependencyObject d, object baseValue)
        {
            return (Brush)baseValue;
        }

        private static void OnLineStrokePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((RaClickableLine)d).SetValue(LineStrokeProperty, (Brush)e.NewValue);

        }


public RaClickableLine()
        {
            SetValue(FrameworkElement.NameProperty, "ClickableLine");
            Background = Brushes.AliceBlue;
            SnapsToDevicePixels = true;

            myLine.Stroke = Brushes.Blue;
            myLine.X1 = 0;
            myLine.X2 = ActualWidth;
            myLine.Y1 = 10;
            myLine.Y2 = 10;


            Binding b_width = new Binding();
            b_width.ElementName = "ClickableLine";
            b_width.Path = new PropertyPath("Width");
            b_width.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
            myLine.SetBinding(Line.X2Property, b_width);

            Children.Add(myLine);
        }

它甚至不告诉我这条线。 如果我离开b_width.ElementName = "ClickableLine"; 并添加DataContext = this;,则会出现该行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-11
    • 1970-01-01
    • 1970-01-01
    • 2011-07-05
    相关资源
    最近更新 更多