4

The Question: By themselves, properties like Grid.Row, Grid.Column, Canvas.SetTop, etc. have great DesignTime support. You attach them to a child element, and watch the xaml update. How does their implementation differ from my example below?

The Example:

In this example I create an attached property called position. I can attach the position property to any child element in a grid. Doing so updates their row and column.

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"/>

This works at run time, but not at design time. My best guess is that maybe OnPositionChanged is not called at design time?

Things I've tried:

  • calling a function when the attribute changes (see example above)
  • adding attributes like FrameworkPropertyMetadataOptions.AffectsRender
  • overriding the OnItemsChanged() function of an ItemsControl
bwall
  • 984
  • 8
  • 22

1 Answers1

0

I don't know where you've put your declarations but the following works:

The window:

<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>

The control:

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
    }
}

Here, I can see changes at design-time :)

Example 1:

enter image description here

Example 2:

enter image description here

aybe
  • 15,516
  • 9
  • 57
  • 105