简体   繁体   English

在XAML中使用全局样式从类型'type'设置属性

[英]Set a property from type 'type' with global style in XAML

I would like to set a dependency property from type 'Type' in xaml. 我想从xaml中的类型'Type'设置依赖项属性。 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). 我创建了一个空的Silverlight应用程序,并添加了一个用户控件(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: 这里是XAML代码:

<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. 如果我删除“ TypeToReflect” -Setter,那么一切正常。 The global styling for the title property works fine too. title属性的全局样式也可以正常工作。

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 : 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. InnerException为null。

You can write: 你可以写:

{x:Type Type}

No more text. 没有更多的文字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM