我有一个在运行时运行良好的附加属性,我也想在设计时使用它。我已经阅读了一些文档并遵循了一些教程,但我认为他们比我更熟悉。
此DP的目的(来自source blog post (microsoft.co.il))是:
如何让这个属性在设计器中与在运行时具有相同的效果?它会影响布局,所以我认为重要的是看看那里发生了什么。
简化的XAML:
<StackPanel Orientation="Horizontal" local:PanelItems.Margin="0,0,10,0">
<TextBox Width="120" Text="{Binding Email}"/>
<Button Content="Search" Command="{Binding SearchCommand}"/>
</StackPanel>
当前类(在运行时正常工作)。
我已经删除了从DependencyObject
继承,因为这对于这种情况没有帮助(我正在关注msdn示例)。我想这些例子不处理附加属性。
public static class PanelItems : DependencyObject
{
public static readonly DependencyProperty MarginProperty =
DependencyProperty.RegisterAttached(
"Margin", typeof(Thickness), typeof(PanelItems),
new UIPropertyMetadata(new Thickness(), MarginChangedCallback)
);
public static Thickness GetMargin(DependencyObject obj)
{
return (Thickness)obj.GetValue(MarginProperty);
}
public static void SetMargin(DependencyObject obj, Thickness value)
{
obj.SetValue(MarginProperty, value);
}
public static void MarginChangedCallback(object sender, DependencyPropertyChangedEventArgs e)
{
// requires a panel control (e.g. a StackPanel)
var panel = sender as Panel;
if (panel == null) return;
panel.Loaded += new RoutedEventHandler(OnPanelLoaded);
}
static void OnPanelLoaded(object sender, RoutedEventArgs e)
{
var panel = sender as Panel;
foreach (var child in panel.Children.OfType<FrameworkElement>())
child.Margin = PanelItems.GetMargin(panel);
}
}
我添加了一个扩展方法来检查是否为给定对象设置了值,这样只允许直接子对象。没有其他方法可以看到,因为没有上下文回到导致级联的原始更改。
public static class DependencyExtensions
{
/// <summary>
/// Checks if a DependencObject has a value set for the Dependency Property.
/// Element is an object for conveninence purpose, and the return value is
/// always false if it is not a DependencyObject (e.g. a UI element)
/// </summary>
/// <param name="property">The DependencyProperty to check</param>
/// <param name="element">The element (a DependencyObject) to check against</param>
/// <returns></returns>
public static bool IsSetFor(this DependencyProperty property, object element)
{
if (element is DependencyObject d)
{
return d.ReadLocalValue(property) != DependencyProperty.UnsetValue;
}
return false;
}
}
之后我稍微修改了一下这个事件。嵌套面板也可以正常工作;内部面板根据外部面板规则,内部儿童获得正确的边距。
if (sender is FrameworkElement el && MarginProperty.IsSetFor(el.Parent))
{
el.Margin = (Thickness)args.NewValue;
}
注意:也可以调用GetMargin(el as DependencyObject)
,尽管这些值与儿童的NewValue
相同。
最后一点说明:我已经关闭了设计师project code
,所以无论我做了什么,我都看不到那里的变化。这是一个带有巨大效果的微小的无标签按钮:
答案 0 :(得分:3)
这是一个带有值继承的Margin属性的快速草图,因此会自动应用于所有子元素。
除了所有子元素之外,边距也适用于Panel本身,您可能会以某种方式避免(可能只是通过检查目标元素是否为Panel)。
public static class PanelItems
{
public static readonly DependencyProperty MarginProperty =
DependencyProperty.RegisterAttached(
"Margin", typeof(Thickness), typeof(PanelItems),
new FrameworkPropertyMetadata(new Thickness(),
FrameworkPropertyMetadataOptions.Inherits, MarginChangedCallback));
public static Thickness GetMargin(DependencyObject obj)
{
return (Thickness)obj.GetValue(MarginProperty);
}
public static void SetMargin(DependencyObject obj, Thickness value)
{
obj.SetValue(MarginProperty, value);
}
private static void MarginChangedCallback(
object sender, DependencyPropertyChangedEventArgs e)
{
var element = sender as FrameworkElement;
if (element != null && !(element is Panel))
{
element.Margin = (Thickness)e.NewValue;
}
}
}