Skip to content

Instantly share code, notes, and snippets.

[TestFixture]
public class LetsCheckTheEventHandlers
{
[Test]
public void ChainedEventHandlers()
{
var m = new MultiplyByTwo();
var s = new CreateResultStars(m);
Assert.AreEqual(string.Empty, s.Stars);
dpd.AddValueChanged(_multiplier, (o, e) => RecalculateStars());
var dpd = DependencyPropertyDescriptor.FromProperty(MultiplyByTwo.ResultProperty, typeof(MultiplyByTwo));
public class CreateResultStars
{
private readonly MultiplyByTwo _multiplier;
public string Stars { get; private set; }
public CreateResultStars(MultiplyByTwo multiplier)
{
_multiplier = multiplier;
public class MultiplyByTwo : DependencyObject
{
private static readonly DependencyPropertyKey ResultPropertyKey =
DependencyProperty.RegisterReadOnly(nameof(Result), typeof(int), typeof(MultiplyByTwo),
new PropertyMetadata(0));
public static readonly DependencyProperty NumberProperty =
DependencyProperty.Register(nameof(Number), typeof(int), typeof(MultiplyByTwo),
new PropertyMetadata(0, CalculateResult));
public static readonly DependencyProperty NumberProperty =
DependencyProperty.Register(nameof(Number),
typeof(int),
typeof(MultiplyByTwo),
new PropertyMetadata(0, CalculateResult));
public static readonly DependencyProperty AProperty =
DependencyProperty.Register(nameof(A),
typeof(int),
typeof(AddCalculation),
new PropertyMetadata(0, AddNumbers));
private static void AddNumbers(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var ac = (AddCalculation)d;
ac.SetValue(ResultPropertyKey, ac.A + ac.B);
}
[TestFixture]
public class ReadOnlyDependencyPropertiesShould
{
[Test]
public void BeReadableByAnyone()
{
var ac = new AddCalculation
{
A = 42
};
public int Result
{
get { return (int)GetValue(ResultProperty); }
}
public static readonly DependencyProperty ResultProperty =
ResultPropertyKey.DependencyProperty;