[英]WPF Attached Property has no effect at design time (xaml editor)
我有一個在運行時運行良好的附加屬性,我也想在設計時使用它。 我已經閱讀了一些文檔並遵循了一些教程,但我認為他們比我更熟悉。
這個DP的目的(來自博客文章(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;
}
注意:雖然值與子項的NewValue
相同,但也可以調用GetMargin(el as DependencyObject)
。
最后一點說明:我已經關閉了設計師的project code
,所以無論我做了什么,我都看不到那里的變化。 這是一個微小的無標簽按鈕,具有巨大的效果:
這是帶有值繼承的Margin屬性的快速草圖,因此自動應用於所有子元素。
除了所有子元素之外,邊距也適用於面板本身,您可能會以某種方式避免(可能只是通過檢查目標元素是否為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;
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.