Silverlight XamlParseException-属性的属性值(…)无效

Silverlight XamlParseException-属性的属性值(…)无效,silverlight,xaml,coding-style,Silverlight,Xaml,Coding Style,我在设置我构建的自定义控件的样式时遇到了一些问题。以下是控制源: namespace SilverlightStyleTest { public class AnotherControl: TextBox { public string MyProperty { get; set; } } } 在同一名称空间和项目中,我尝试为MyProperty创建一个带有setter的样式,如下所示: <UserControl x:Class="Silverli

我在设置我构建的自定义控件的样式时遇到了一些问题。以下是控制源:

namespace SilverlightStyleTest
{
    public class AnotherControl: TextBox
    {
        public string MyProperty { get; set; }
    }
}
在同一名称空间和项目中,我尝试为MyProperty创建一个带有setter的样式,如下所示:

<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>

最后出现运行时错误: 属性的属性值MyProperty无效。[第9行位置:30]


我不知道是什么原因导致了这个错误。我还尝试将属性名“完全限定”为“Local:AnotherControl.MyProperty”,但这也不起作用。

无法在样式中设置非依赖属性

您需要将其定义为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); }
}

您的类名为
另一个文本框
,但您的XAML引用了
另一个控件
。那是哪一个呢?请相应地更正问题。我假设在隐藏您的姓名时输入错误,但您在下面的示例中使用了“AnotherControl”,而不是顶部的“AnotherTextbox”。体验并打开VS,我亲爱的Watson;)