我有這樣一個類:我需要做什麼才能使屬性元素語法適用於Silverlight中的自定義附加屬性?
public class Stretcher : Panel {
public static readonly DependencyProperty StretchAmountProp = DependencyProperty.RegisterAttached("StretchAmount", typeof(double), typeof(Stretcher), null);
public static void SetStretchAmount(DependencyObject obj, double amount)
{
FrameworkElement elem = obj as FrameworkElement;
elem.Width *= amount;
obj.SetValue(StretchAmountProp, amount);
}
}
我可以使用屬性語法在XAML中設置的伸縮量屬性:
<UserControl x:Class="ManagedAttachedProps.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:map="clr-namespace:ManagedAttachedProps"
Width="400" Height="300">
<Rectangle Fill="Aqua" Width="100" Height="100" map:Stretch.StretchAmount="100" />
</UserControl>
,我的矩形拉長,但我不能使用屬性像這樣的元素語法:
<UserControl x:Class="ManagedAttachedProps.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:map="clr-namespace:ManagedAttachedProps"
Width="400" Height="300">
<Rectangle Fill="Aqua" Width="100" Height="100">
<map:Stretcher.StretchAmount>100</map:Stretcher.StretchAmount>
</Rectangle>
</UserControl>
與屬性元素語法我的組塊似乎被完全忽略(我甚至可以把無效的雙值),SetStretchAmount方法永遠不會被調用。
我知道可以這樣做,因爲VisualStateManager可以做到這一點。我試過使用除雙以外的類型,但似乎沒有任何工作。
很有意思,有些奇怪。當我有機會訪問一臺Windows機器時,我會明天再試一次,並確保在其工作時給出答案。謝謝! – Jacksonh 2008-11-12 06:10:49