我有这样的课:
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>
使用属性元素语法,我的 set 块似乎完全被忽略了(我什至可以在其中放置无效的双精度值),并且永远不会调用 SetStretchAmount 方法。
我知道可以做这样的事情,因为 VisualStateManager 做到了。我尝试过使用 double 以外的类型,但似乎没有任何效果。