1

I would like to set a dependency property from type 'Type' in xaml. This will work fine but when I set this value in an implicit or explicit styling then a exception will thrown (unhandled exception).

I created an empty Silverlight application and added a user control (DataFormControl). Here is the code behind of this control:

    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();
    }

And here the XAML code:

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

Now the problem occurres in a control which reference this and use global styling:

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

If I remove the "TypeToReflect"-Setter then all work fine. The global styling for the title property works fine too.

Is this a bug or is there a workaround?

I need the type because I would like to use reflection on it.

Edit:

Exception information:

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 is null.

Patrick D'Souza
  • 3,491
  • 2
  • 22
  • 39
Klaus
  • 189
  • 2
  • 11
  • What is the `InnerException` of the exception? How does the stack trace look like? – svick Apr 19 '12 at 08:11
  • http://stackoverflow.com/questions/6028276/missing-style-triggers-and-xtype-why You're right. There's a similar solution. – Jens Apr 19 '12 at 08:40
  • Take a look at this answer to a similar question https://stackoverflow.com/a/677285/1161647 – Andrew Jones Apr 19 '12 at 08:40
  • On this example the type of the style will define. This works for me currently. I can use explicit and implicit styling. But the Setter line for the TypeToReflect-Property not work. I can not find an explain on the referenced link. – Klaus Apr 19 '12 at 09:06

1 Answers1

0

You can write:

{x:Type Type}

No more text.

Jens
  • 505
  • 5
  • 17