我在Windows Phone Mango應用程序中依賴項屬性有點麻煩。這裏有兩個控件,其字體大小,我想動態改變:依賴屬性困難
<TextBlock Text="{Binding}" FontSize="{Binding ElementName=ParagraphItems, Path=DataContext.TextScale}" />
<local:HyperlinkTextBlock Text="{Binding}" FontSize="{Binding ElementName=ParagraphItems, Path=DataContext.TextScale}" />
的TextBlock
工作正常,但HyperlinkTextBlock
沒有。 HyperlinkTextBlock
是我做了一個類:
<UserControl
<!-- ... -->
>
<RichTextBox x:Name="LayoutRoot" TextWrapping="Wrap" FontSize="{Binding FontSize}">
<Paragraph x:Name="BaseParagraph" />
</RichTextBox>
</UserControl>
public partial class HyperlinkTextBlock : UserControl { /* ... */ }
我不知道我需要在HyperlinkTextBlock
做使它所以當它在XAML聲明它可以接收FontSize
值。我試着在HyperlinkTextBlock.xaml
結合的財產,並通知當屬性更改後臺代碼:
public new double FontSize
{
get
{
return base.FontSize;
}
set
{
base.FontSize = value;
onPropChanged("FontSize");
}
}
(?這是new
因爲UserControl
已經有一個FontSize
財產 - 我不應該只是能使用)
我也嘗試創建一個全新的依賴屬性:
public static readonly new DependencyProperty FontSizeProperty = DependencyProperty.RegisterAttached(
"FontSize",
typeof(double),
typeof(HyperlinkTextBlock),
new PropertyMetadata(20, new PropertyChangedCallback(onFontSizeChanged)));
public new double FontSize
{
get { return (double)GetValue(FontSizeProperty); }
set { SetValue(FontSizeProperty, value); }
}
private static void onFontSizeChanged(DependencyObject dependObj, DependencyPropertyChangedEventArgs e)
{
((HyperlinkTextBlock)dependObj).LayoutRoot.FontSize = (double)e.NewValue;
}
同樣,這並不工作。在運行時,它給出了錯誤:
System.ArgumentException was unhandled
Message=Default value type does not match type of property.
StackTrace:
at System.Windows.DependencyProperty.Register(Boolean fIsAttachedDP, String name, Type propertyType, Type ownerType, PropertyMetadata propertyMetadata, Boolean readOnly)
at System.Windows.DependencyProperty.RegisterAttached(String name, Type propertyType, Type ownerType, PropertyMetadata defaultMetadata)
at MyApp.Views.HyperlinkTextBlock..cctor()
at System.Reflection.RuntimeConstructorInfo.InternalInvoke(RuntimeConstructorInfo rtci, BindingFlags invokeAttr, Binder binder, Object parameters, CultureInfo culture, Boolean isBinderDefault, Assembly caller, Boolean verifyAccess, StackCrawlMark& stackMark)
at System.Reflection.RuntimeConstructorInfo.InternalInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, StackCrawlMark& stackMark)
at System.Reflection.ConstructorInfo.Invoke(Object[] parameters)
at MS.Internal.TypeProxy.<>c__DisplayClass30.<GetCreateObjectDelegate>b__2a()
at MS.Internal.TypeProxy.CreateInstance(UInt32 customTypeId)
at MS.Internal.XamlManagedRuntimeRPInvokes.CreateInstance(XamlTypeToken inXamlType, XamlQualifiedObject& newObject)
什麼是正確的方式去做到這一點?
更新:
如果我只設置FontSize
直接HyperlinkTextBlock
:
<local:HyperlinkTextBlock Text="{Binding}" Margin="0,15" FontSize="33.0" />
<local:HyperlinkTextBlock Text="{Binding}" Margin="0,15" FontSize="40" />
而且從HyperlinkTextBlock
自身刪除有關FontSize
什麼:
<RichTextBox x:Name="LayoutRoot" TextWrapping="Wrap">
<Paragraph x:Name="BaseParagraph" />
</RichTextBox>
那麼有沒有觀察到的影響設置字體大小。 (以上聲明的兩個文本塊出現相同。)
這工作。謝謝。 (有沒有什麼方法可以使用繼承的'FontSize'而不是定義我自己的版本?) –
添加了一個方法來實現這個答案。 –