在 WPF 中绑定两个依赖属性

发布于 2024-12-08 16:01:02 字数 173 浏览 4 评论 0原文

我声明了两个依赖属性: 首先,Color类型的FilterColor 第二个 FilterBrush 类型为 Brush。 当 FilterBrush.Color 属性更改时,我需要更新 FilterColor 的值;当 FilterColor 属性更改时,我需要更新 FilterBrush.Color 的值。 我怎样才能实现它?

I declare two Dependency properties:
first, FilterColor of type Color
and second FilterBrush of type Brush.
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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夏花。依旧 2024-12-15 16:01:02

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

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.

你的往事 2024-12-15 16:01:02

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

例如....

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

我可能有一些语法错误... 我没有编译器来执行此 操作检查代码

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

年华零落成诗 2024-12-15 16:01:02

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

如果它在视图模型中,您应该使用 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,而且非常糟糕,我更喜欢第一种方法。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文