开发者

Binding two Dependency properties in WPF

I declare two Dependency properties: first, FilterColor of type Color and second FilterBrush of type Br开发者_StackOverflow社区ush. I need to update value of FilterColor when FilterBrush.Color property has changed, and I need to update value of FilterBrush.Color when FilterColor property has changed. How I can realize it?


Bind your two properties with TwoWay binding and if you change one in the UI change the other in the properties setter and Vice Versa, and use INotifyPropertyChanged to Notify your UI that the property changed.


You can either do it in the DependencyProperty definition, or you can use a DependencyPropertyDescriptor to do it afterwards

For example....

DependencyProperty Definition:

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 Descriptor:

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;
}

I may have a few syntax errors... I don't have a compiler to check the code


Where did you define these properties: in view model or in control?

If it's in view model you should use INotifyPropertyChanged instead of dependency properties like this:

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);
}

If it is in control do this:

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;
}

Note that last way is just hack and it is really bad way, I would prefer the first way.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜