在WPF中绑定两个依赖属性

本文关键字:两个 依赖 属性 WPF 绑定 | 更新日期: 2023-09-27 18:10:39

我声明了两个Dependency属性:首先是Color类型的FilterColor第二个类型为Brush的FilterBrush。我需要在FilterBrush时更新FilterColor的值。Color属性改变了,我需要更新FilterBrush的值。FilterColor属性改变时的颜色。我怎么能实现呢?

在WPF中绑定两个依赖属性

用双向绑定绑定您的两个属性,如果您在UI中更改了一个,则在属性设置器中更改另一个,反之亦然,并使用INotifyPropertyChanged来通知您的UI属性更改。

您可以在DependencyProperty定义中执行此操作,也可以在之后使用DependencyPropertyDescriptor执行此操作

例如…

DependencyProperty定义:

public static readonly DependencyProperty FilterColorProperty =
    DependencyProperty.Register("FilterColor", typeof(Color),
    typeof(MyUserControl),
    new PropertyMetadata(Colors.White, FilterColorChanged));
public static void FilterColorChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
    if (!(obj is MyUserControl))
        return;
    MyUserControl ctrl = (MyUserControl)obj;
    var brush = ctrl.GetBrushProperty();
    if (brush.Color != (Color)e.NewValue)
        brush.Color = (Color)e.NewValue;
}

DependencyProperty描述符:

DependencyPropertyDescriptor dpd = DependencyPropertyDescriptor.FromProperty(
    MyUserControl.FilterColorProperty, typeof(MyUserControl));
if (dpd != null) 
    dpd.AddValueChanged(this, delegate { FilterColor_Changed(); });
...
private void FilterColor_Changed()
{
    Color filterColor = GetFilterColor(this);
    var brush = GetBrush(this);
    if (filterColor != brush.Color)
        brush.Color = filterColor;
}

我可能有一些语法错误…我没有编译器来检查代码

您在哪里定义这些属性:在视图模型中还是在控件中?

如果它在视图模型中,你应该使用INotifyPropertyChanged而不是像这样的依赖属性:

Color _filterColor;
public Color FilterColor
{
    get
    {
        return _filterColor;
    }
    {
        if (_filterColor != value)
        {
            _filterColor = value;
            RaisePropertyChanged(() => FilterColor);
            _OnFilterColorChanged();
        }
}
void _OnFilterColorChanged()
{
    _filterBrush= ...
    RaisePropertyChanged(() => FilterBrush);
}
Brush _filterBrush;
public Brush FilterBrush
{
    get
    {
        return _filterBrush;
    }
    {
        if (_filterBrush != value)
        {
            _filterBrush = value;
            RaisePropertyChanged(() => FilterBrush);
            _OnFilterBrushChanged();
        }
}
void _OnFilterBrushChanged()
{
    _filterColor= ...
    RaisePropertyChanged(() =. FilterColor);
}

如果它在控制中,请这样做:

public Color FilterColor
{
    get { return (Color)GetValue(FilterColorProperty); }
    set { SetValue(FilterColorProperty, value); }
}
public static readonly DependencyProperty FilterColorProperty =
    DependencyProperty.Register("FilterColor", typeof(Color), typeof(MainWindow), new UIPropertyMetadata(Colors.Transparent, new PropertyChangedCallback(_OnFilterColorPropertyChanged)));
static void _OnFilterColorPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    var mw = d as MainWindow;
    Color oldValue = (Color)e.OldValue;
    Color newValue = (Color)e.NewValue;
    if (null != mw && oldValue != newValue)
    {
        mw._OnFilterColorChanged(oldValue, newValue);
    }
}
bool _isFilterColorUpdating = false;
void _OnFilterColorChanged(Color oldValue, Color newValue)
{
    if (_isFilterBrushUpdating )
        return;
    _isFilterColorUpdating = true;
    Brush = ...
    _isFilterColorUpdating = false;
}
public Brush FilterBrush
{
    get { return (Brush)GetValue(FilterBrushProperty); }
    set { SetValue(FilterBrushProperty, value); }
}
public static readonly DependencyProperty FilterBrushProperty =
    DependencyProperty.Register("FilterBrush", typeof(Brush), typeof(MainWindow), new UIPropertyMetadata(Brushs.Transparent, new PropertyChangedCallback(_OnFilterBrushPropertyChanged)));
static void _OnFilterBrushPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    var mw = d as MainWindow;
    Brush oldValue = (Brush)e.OldValue;
    Brush newValue = (Brush)e.NewValue;
    if (null != mw && oldValue != newValue)
    {
        mw._OnFilterBrushChanged(oldValue, newValue);
    }
}
bool _isFilterBrushUpdating = false;
void _OnFilterBrushChanged(Brush oldValue, Brush newValue)
{
    if (_isFilterColorUpdating )
        return;
    _isFilterBrushUpdating = true;
    Color = ...
    _isFilterBrushUpdating = false;
}

请注意,最后一种方式只是hack,这是一个非常糟糕的方式,我更喜欢第一种方式