[英]Set a property from type 'type' with global style in XAML
我想從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>
現在,問題出現在引用此引用並使用全局樣式的控件中:
<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。
你可以寫:
{x:Type Type}
沒有更多的文字。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.