user1031200
user1031200

Reputation:

Why does Visual Studio prompt error in XAML when I use Label?

I use Visual Studio 2012, and wrote some XAML(of WPF) code, just like this:

<StackPanel>
    <Label Target="txt">UserName:</Label>
    <TextBox Name="txt"></TextBox>
</StackPanel>

then, the compilation failed. It said to Label related-code,

Value cannot be null. Parameter name: context.

I don't know its meaning. Did I miss anything?

Upvotes: 5

Views: 838

Answers (1)

Sisyphe
Sisyphe

Reputation: 4684

It is a designer-only error. Code should compile without any issue.

EDIT: This exception is due to Silverlight not supporting this syntax : Target="txt". this syntax is only supported by WPF. It's a known bug in the designer, even if your project is a WPF project, and was caused by MS wanting to support design-time checks for Silverlight.

If you don't want the exception you have to use the full binding syntax (supported by WPF and Silverlight)

<Label Target="{Binding ElementName=txt}">UserName:</Label>

Upvotes: 5

Related Questions