How can I create a readable Boolean dependency property that returns an AND operation between two other dependency properties

How to create a custom Boolean dependency property for reading that returns an operation Andbetween two custom Boolean dependency properties, e.g. (A, B),

And when A or B change, I want the result property to call.

Any help to achieve this!

+3
source share
2 answers

Part 1: dependencies.

public static readonly DependencyProperty Source1Property =
    DependencyProperty.Register(
        "Source1",
        typeof(bool),
        typeof(MyControl),
        new FrameworkPropertyMetadata(false, new PropertyChangedCallback(UpdateTarget)));

public bool Source1
{
    get { return (bool)GetValue(Source1Property); }
    set { SetValue(Source1Property, value); }
}

void UpdateTarget(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    MyControl self = (MyControl)d;
    d.Target = d.Source1 && d.Source2;
}

Part 2: Read Only

internal static readonly DependencyPropertyKey TargetPropertyKey =
    DependencyProperty.RegisterReadOnly(
        "Target",
        typeof(bool),
        typeof(MyControl),
        new PropertyMetadata(false));

public static readonly DependencyProperty TargetProperty =
    TargetPropertyKey.DependencyProperty;

public bool Target
{
    get { return (bool)GetValue(TargetProperty); }
    protected set { SetValue(TargetPropertyKey, value); }
}

Disclaimer: I have not tried part 2.

Part 3:
if the properties of the original dependency are not defined by you, you can do the following trick:

DependencyPropertyDescriptor dpd = DependencyPropertyDescriptor.FromProperty(
        MyControl.Source1Property,
        typeof(MyControl)));
if (dpd != null)
    dpd.AddValueChanged(this, UpdateTarget);
+5

, A B ( , ), , , , PropertyMetaData DependencyProperty. Result depdendency. :

public class Data : DependencyObject
{
    public static readonly DependencyProperty AProperty = DependencyProperty.Register("A", typeof(Boolean), typeof(Data), new PropertyMetadata(false,HandleValueChanged));
    public static readonly DependencyProperty BProperty = DependencyProperty.Register("B", typeof(Boolean), typeof(Data), new PropertyMetadata(false, HandleValueChanged));
    public static readonly DependencyProperty ResultProperty = DependencyProperty.Register("Result",typeof (Boolean), typeof (Data));

    private static void HandleValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        d.SetValue(ResultProperty, ((Data)d).Result);
        Debug.WriteLine("Value change");
    }

    public bool Result
    {
        get { return A & B; }
    }

    public bool A
    {
        get { return (bool) GetValue(AProperty); }
        set
        {
            if ( A != value )
                SetValue(AProperty, value);
        }
    }
    public bool B
    {
        get
        {
            return (bool) GetValue(BProperty);
        }
        set
        {
            if (B != value)
                SetValue(BProperty, value);
        }
    }
}
+3

Source: https://habr.com/ru/post/1773485/


All Articles