XamlParseException - Invalid attribute value (...) for Property

I'm having trouble setting up the custom control that I created. Here is the control source:

namespace SilverlightStyleTest
{
    public class AnotherControl: TextBox
    {
        public string MyProperty { get; set; }
    }
}

In the same namespace and project, I am trying to create an installer style for MyProperty, for example:

<UserControl x:Class="SilverlightStyleTest.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:Local="clr-namespace:SilverlightStyleTest">

    <UserControl.Resources>
        <Style x:Name="AnotherStyle" TargetType="Local:AnotherControl">
            <Setter Property="Width" Value="200"/>
            <Setter Property="MyProperty" Value="Hello."/>
        </Style>
    </UserControl.Resources>

    <Grid x:Name="LayoutRoot">
        <Local:AnotherControl Style="{StaticResource AnotherStyle}"/>
    </Grid>
</UserControl>

I get a runtime error: Invalid MyProperty attribute value for Property property. [Line: 9 Position: 30]

I can’t understand what happened to the style in order to cause this error. I also tried to "fully qualify" the property name as "Local: AnotherControl.MyProperty", but that didn't work either.

+3
source share
1 answer

- .

DependencyProperty:

public static readonly DependencyProperty MyPropertyProperty =
    DependencyProperty.Register("MyProperty", typeof(string), typeof(AnotherTextBox),
        new FrameworkPropertyMetadata((string)null));

public string MyProperty
{
    get { return (string)GetValue(MyPropertyProperty); }
    set { SetValue(MyPropertyProperty, value); }
}
+4

Source: https://habr.com/ru/post/1714333/


All Articles