Marius
Marius

Reputation: 9674

How do I set the target property on a label?

The following code:

    <TextBlock Name="foo"></TextBlock>
    <Label Target="foo">_Delta pressure</Label>

Generates the following design time error:

Error 1 Value 'foo' cannot be assigned to property 'Target'. Property 'Target' of type 'UIElement' cannot be specified as a string. C:\Programming\WpfCustomPlot\SPT.Olga.Plot.Custom\PumpCurves\View\RatedValuesView.xaml 65 45 SPT.Olga.Plot.Custom

And the following runtime error:

'UIElement' type does not have a public TypeConverter class. Error at Line 65 Position 45.

What am I doing wrong?

Upvotes: 21

Views: 9757

Answers (2)

CptRobby
CptRobby

Reputation: 1521

It appears that in version 4.0 of the .NET Framework, this property was changed so that it would be able to just take the name of the element as a string. This was accomplished by decorating the Label.Target property with a TypeConverterAttribute using a NameReferenceConverter to handle the conversion from String to UIElement.

Check out the following docs for more info:

https://msdn.microsoft.com/en-us/library/system.windows.controls.label.target(v=vs.100).aspx

https://msdn.microsoft.com/en-us/library/system.windows.markup.namereferenceconverter(v=vs.100).aspx

PS: Note that it is generally recognized as better practice to use the x:Name attribute to specify element names in XAML rather than the Name attribute.

Upvotes: 5

Kent Boogaart
Kent Boogaart

Reputation: 178710

The Target property takes the element itself, not a string, so you want:

<TextBlock Name="foo"></TextBlock>
<Label Target="{Binding ElementName=foo}">_Delta pressure</Label>

Upvotes: 39

Related Questions