문제

I just stumbled over the following:

class AFrameworkElement : FrameworkElement
{
    private void SomeMethod() 
    {
        Size s = new Size {
            Width = Height = 10
        };
        // the size isn't used for measuring oder arranging
    }
}

Suddenly I was unable to resize the Control... IntelliSense showed the problem: I put he mouse over Height and it showed that the FrameworkElement.Height property was meant. So why is it impossible to use double assignements in object initializers?

도움이 되었습니까?

해결책

In an object initializer expression the object itself is not accessible within the initialization expression. Anything used in that expression is interpreted as if it were used outside the object initializer. Hence in this case Height = 10 is evaluated within the context of AFrameworkElement and Height binds to a property on that object

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top