2012-04-19 4 views
1

xamlのタイプ 'Type'から依存プロパティを設定したいと思います。これは正常に動作しますが、暗黙的または明示的なスタイリングでこの値を設定すると、例外がスローされます(処理されない例外)。XAMLのグローバルスタイルで 'type'タイプのプロパティを設定する

空のSilverlightアプリケーションを作成し、ユーザーコントロール(DataFormControl)を追加しました。

public DataFormControl() 
    { 
     InitializeComponent(); 
    } 

    public static readonly DependencyProperty TitleProperty = DependencyProperty.Register("Title", typeof(string), typeof(DataFormControl), null); 
    public string Title 
    { 
     get { return (string)GetValue(TitleProperty); } 
     set { SetValue(TitleProperty, value); } 
    } 

    public static readonly DependencyProperty TypeToReflectProperty = DependencyProperty.Register("TypeToReflect", typeof(Type), typeof(DataFormControl), null); 
    public Type TypeToReflect 
    { 
     get { return (Type)GetValue(TypeToReflectProperty); } 
     set { SetValue(TypeToReflectProperty, value); } 
    } 

    public string GetCombo() 
    { 
     string returnValue = (Title ?? "no title") + " ; " + (TypeToReflect != null ? TypeToReflect.Name : "unkown Type"); 
     return returnValue; 
    } 


    private void Refresh_Button(object sender, RoutedEventArgs e) 
    { 
     this.ResultBox.Text = GetCombo(); 
    } 

そしてここでXAMLコード:

<Grid x:Name="LayoutRoot"> 
    <StackPanel Orientation="Horizontal"> 
     <Button Click="Refresh_Button">Refresh</Button>    
     <TextBlock x:Name="ResultBox" /> 
    </StackPanel> 
</Grid> 

さて問題は、これを参照するコントロールにoccurresとグローバルスタイリングを使用します。

<StackPanel> 
     <StackPanel.Resources> 
      <Style TargetType="local:DataFormControl"> 
       <Setter Property="Title" Value="Implicit Name" /> 
       <Setter Property="TypeToReflect" Value="local:DataFormControl" /> 
      </Style> 
     </StackPanel.Resources> 
     <TextBlock FontWeight="Bold">Test App</TextBlock> 

     <local:DataFormControl Title="123" /> 
     <local:DataFormControl Title="Kuh" /> 
     <local:DataFormControl TypeToReflect="local:DataFormControl" /> 
     <local:DataFormControl /> 
    </StackPanel> 
ここ は、このコントロールの背後にあるコードであります

"TypeToReflect" -Setterを削除しても問題ありません。 titleプロパティのグローバルなスタイリングもうまくいきます。

これはバグですか?それとも回避策がありますか?

私はそれに反射を使用したいのでタイプが必要です。

編集:

例外情報:

Message is always. [Line: 0 Position: 0] 
ExceptionType: Unhandled Exception 
ExceptionObject: XamlParseException 

STACKTRACE

at MS.Internal.XcpImports.CheckHResult(UInt32 hr) 
    at MS.Internal.XcpImports.ConvertStringToTypedCValue(IntPtr pContext, UInt32 cClrTypeName, String clrTypeName, UInt32 cValue, String value, CValue& outVal, Int32& typeIndex) 
    at MS.Internal.SilverlightTypeConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value) 
    at MS.Internal.XcpImports.GetManagedPropertyValueFromStyle(Boolean useBuiltInStyle, IManagedPeerBase obj, DependencyProperty property, Object& value) 
    at System.Windows.FrameworkElement.GetValueFromStyle(DependencyProperty property, Object& value) 
    at System.Windows.DependencyObject.EvaluateBaseValue(DependencyProperty property, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, ValueOperation operation) 
    at System.Windows.DependencyObject.EvaluateEffectiveValue(DependencyProperty property, EffectiveValueEntry oldEntry, EffectiveValueEntry newEntry, ValueOperation operation) 
    at System.Windows.DependencyObject.UpdateEffectiveValue(DependencyProperty property, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, ValueOperation operation) 
    at System.Windows.DependencyObject.InvalidateProperty(DependencyProperty property) 
    at MS.Internal.FrameworkCallbacks.InvalidateProperty(IntPtr nativeTarget, UInt32 propertyId) 

のInnerExceptionはnullです。

+0

例外の 'InnerException'は何ですか?スタックトレースはどのように見えますか? – svick

+0

http://stackoverflow.com/questions/6028276/missing-style-triggers-and-xtype-why あなたは正しいです。同様の解決策があります。 – Jens

答えて

0

同様の質問にこの答えを見てみましょうhttps://stackoverflow.com/a/677285/1161647

+0

この例では、スタイルのタイプが定義されます。これは現在私のために働いています。明示的かつ暗黙的なスタイリングを使用することができます。しかし、TypeToReflectプロパティのSetter行は機能しません。私は参照されたリンクの説明を見つけることができません。 – Klaus

0

あなたは書くことができます。

{x:Type Type} 

これ以上のテキストを。

+0

シルバーライトで私は思ったx:Typeキーワードを使うことはできません。 – Klaus

関連する問題