WPF – 更改另一个 属性 的 Setter 中的 属性 值
WPF – Change Property Value in Setter of Another Property
在我的 自定义控件 的以下示例中,为什么我无法更改 TransparentColor 属性 setter 一个 SelectedColor 属性?有回调方法的解决方案工作正常,在改变另一个的情况下它们之间有什么区别属性?
public class MyColorPicker : Control
{
static MyColorPicker()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(MyColorPicker), new FrameworkPropertyMetadata(typeof(MyColorPicker)));
}
public static readonly DependencyProperty SelectedColorProperty =
DependencyProperty.Register("SelectedColor", typeof(Color), typeof(MyColorPicker), new PropertyMetadata(Color.FromRgb(0, 201, 201), OnSelectedColorChange));
private static void OnSelectedColorChange(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
var colorPicker = (MyColorPicker)sender;
var selectedColor = colorPicker.SelectedColor;
colorPicker.TransparentColor = Color.FromArgb(0, selectedColor.R, selectedColor.G, selectedColor.B);
}
public Color SelectedColor
{
get { return (Color)GetValue(SelectedColorProperty); }
set
{
// Why this not working? BorderStopColor = Color.FromArgb(0, value.R, value.G, value.B);
SetValue(SelectedColorProperty, value);
}
}
private static readonly DependencyPropertyKey TransparentColorPropertyKey =
DependencyProperty.RegisterReadOnly("TransparentColor", typeof(Color), typeof(MyColorPicker), new PropertyMetadata(Color.FromArgb(0, 0, 201, 201)));
public static readonly DependencyProperty TransparentColorProperty = TransparentColorPropertyKey.DependencyProperty;
public Color TransparentColor
{
get { return (Color)GetValue(TransparentColorProperty); }
protected set { SetValue(TransparentColorPropertyKey, value); }
}
}
依赖项 属性 的 CLR 包装器的 setter 应该 仅 调用 SetValue
方法来设置依赖性 属性。
任何其他逻辑应该在回调中实现。
另请注意,在 XAML 中设置依赖属性时,CLR 属性 包装器会在运行时被绕过(但回调不会):
Why are .NET property wrappers bypassed at runtime when setting dependency properties in XAML?
在我的 自定义控件 的以下示例中,为什么我无法更改 TransparentColor 属性 setter 一个 SelectedColor 属性?有回调方法的解决方案工作正常,在改变另一个的情况下它们之间有什么区别属性?
public class MyColorPicker : Control
{
static MyColorPicker()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(MyColorPicker), new FrameworkPropertyMetadata(typeof(MyColorPicker)));
}
public static readonly DependencyProperty SelectedColorProperty =
DependencyProperty.Register("SelectedColor", typeof(Color), typeof(MyColorPicker), new PropertyMetadata(Color.FromRgb(0, 201, 201), OnSelectedColorChange));
private static void OnSelectedColorChange(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
var colorPicker = (MyColorPicker)sender;
var selectedColor = colorPicker.SelectedColor;
colorPicker.TransparentColor = Color.FromArgb(0, selectedColor.R, selectedColor.G, selectedColor.B);
}
public Color SelectedColor
{
get { return (Color)GetValue(SelectedColorProperty); }
set
{
// Why this not working? BorderStopColor = Color.FromArgb(0, value.R, value.G, value.B);
SetValue(SelectedColorProperty, value);
}
}
private static readonly DependencyPropertyKey TransparentColorPropertyKey =
DependencyProperty.RegisterReadOnly("TransparentColor", typeof(Color), typeof(MyColorPicker), new PropertyMetadata(Color.FromArgb(0, 0, 201, 201)));
public static readonly DependencyProperty TransparentColorProperty = TransparentColorPropertyKey.DependencyProperty;
public Color TransparentColor
{
get { return (Color)GetValue(TransparentColorProperty); }
protected set { SetValue(TransparentColorPropertyKey, value); }
}
}
依赖项 属性 的 CLR 包装器的 setter 应该 仅 调用 SetValue
方法来设置依赖性 属性。
任何其他逻辑应该在回调中实现。
另请注意,在 XAML 中设置依赖属性时,CLR 属性 包装器会在运行时被绕过(但回调不会):
Why are .NET property wrappers bypassed at runtime when setting dependency properties in XAML?