开发者

How can I create a read-only Boolean dependency property that returns `And` operation between two other dependency properties

How can I create a custom read-only Boolean dependency prope开发者_开发百科rty that returns And operation between two custom Boolean dependency properties, for example (A, B),

And when A or B changes, I want the result property to trigger.

Any help to achieve that!


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 didn't try the part 2.

Part 3:
if the source dependency properties 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);


You can do this by defining your two dependency properties A and B (for the sake of the example, I guess), and define a callback to be executed whenever these changes, using PropertyMetaData in the DependencyProperty constructor. In this callback, simply perform the calculation you want and set the Result depdendency property to that value. Here is a working example:

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜