2009-08-04 56 views
3

私が作成したカスタムコントロールのスタイリングに問題があります。XamlParseException - プロパティのプロパティ値(...)が無効です。

<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ためのセッターとスタイルを作成しようと同じ名前空間&プロジェクトで

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

: 無効な属性ここで制御源ですプロパティPropertyの値MyProperty。 [行:9位置:30]

このエラーの原因となるスタイルには何が問題なのかわかりません。私はまた、 "Local:AnotherControl.MyProperty"としてプロパティ名を "完全修飾"しようとしましたが、それもどちらも機能しませんでした。

+0

あなたのクラスは 'AnotherTextBox'が、あなたのXAMLのリファレンス' AnotherControl'命名されます。それはどちらですか?それに応じて問題を修正してください。 –

+0

私はあなたの名前を隠しているが、上から "AnotherTextbox"ではなく、下の例で "AnotherControl"を使用したとき、そのタイプミスを想定しています。 –

+0

エラー - 入力ミスが修正されました。 –

答えて

4

非依存プロパティは、スタイルでは設定できません。

あなたがた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); } 
} 
+0

数秒でビート! –

+0

すごかった男!スピードの倍のポイント? ;) –

+0

経験とオープンVS、私の親愛なるワトソン;) –

関連する問題