UserControl DataBinding not working
本文关键字:working not DataBinding UserControl | 更新日期: 2023-09-27 18:26:18
嗨,伙计们,我又需要你们的帮助了。
我的XAML:
我想将Line1的Color绑定到XAML中的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中的绑定值?)
非常感谢苏!!!!
首先!谢谢你们,你们已经帮助了我,但它有效,但不完全(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;
,则会出现该行。