问题: Grid.Row,Grid.Column,Canvas.SetTop等属性本身具有很好的DesignTime支持。您将它们附加到子元素,并观察xaml更新。它们的实现与我下面的示例有何不同?
示例:
在此示例中,我创建了一个名为 position 的附加属性。我可以将位置属性附加到网格中的任何子元素。这样做会更新他们的行和列。
public static void SetPosition(DependencyObject obj, Positioning value) => obj.SetValue(PositionProperty, value);
public static void GetPosition(DependencyObject obj) => (Positioning)obj.GetValue(PositionProperty);
public static readonly DependencyProperty PositionProperty = DependencyProperty.RegisterAttatched( "Position", typeof(Positioning),
new PropertyMetadata( Positioning.Normal, OnPositionChanged));
public static void OnPositionChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
UIElement item = obj as UIElement;
if(item == null)
return;
switch((Positioning) e.NewValue)
{
case Positioning.Middle:
Grid.SetRow(item, 4);
Grid.SetColumn(item, 2);
break;
default:
Grid.SetRow(item, 0);
Grid.SetColumn(item, 0);
}
}
//Usage:
<Rectangle local:Position="Middle" Fill="Pink" Height="40" Width="40"/>
这适用于运行时,但不适用于设计时。我最好的猜测是,在设计时可能没有调用OnPositionChanged?
我尝试过的事情:
答案 0 :(得分:0)
我不知道您的声明在哪里,但以下是有效的:
窗口:
<Window
x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WpfApp1"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<local:UserControl1 local:UserControl1.Position="Zero" Background="Red" />
</Grid>
</Window>
控件:
using System.Windows;
using System.Windows.Controls;
namespace WpfApp1
{
public partial class UserControl1
{
public static readonly DependencyProperty PositionProperty = DependencyProperty.RegisterAttached(
"Position", typeof(Position), typeof(UserControl1),
new PropertyMetadata(default(Position), PositionPropertyChangedCallback));
public UserControl1()
{
InitializeComponent();
}
private static void PositionPropertyChangedCallback(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
if (!(o is UIElement uiElement))
return;
var position = (Position) e.NewValue;
var value = (int) position;
Grid.SetRow(uiElement, value);
}
public static void SetPosition(DependencyObject element, Position value)
{
element.SetValue(PositionProperty, value);
}
public static Position GetPosition(DependencyObject element)
{
return (Position) element.GetValue(PositionProperty);
}
}
public enum Position
{
Zero = 0,
One = 1,
Two = 2
}
}
在这里,我可以看到设计时的变化:)
示例1:
示例2: