在设计器视图中重置附加属性

发布于 2024-11-29 00:06:58 字数 1033 浏览 2 评论 0原文

我有一个网格的附加属性。它用于自动布局网格内的内容控件。它的基本作用是遍历 Children 集合并将每个控件放入 Grid 控件的下一个空闲单元格中。它完成一次(在初始化事件处理程序中)。

    public static readonly DependencyProperty AutoLayoutProperty =
        DependencyProperty.RegisterAttached(
            "AutoLayout", 
            typeof(bool), 
            typeof(GridEx), 
            new UIPropertyMetadata(false, OnAutoLayoutChanged));

    private static void OnAutoLayoutChanged(
        DependencyObject d, 
        DependencyPropertyChangedEventArgs e)
    {
        var grid = d as Grid;
        if (!grid.IsInitialized) 
            grid.Initialized += new EventHandler(grid_Initialized);
        else { UpdateLayout(grid); }
    }
    private static void UpdateLayout(Grid grid)
    {
        foreach(var child in grid.Children) 
        {
            // Set Grid.Column and Grid.Row properties on the child
        }
    }

这段代码可以工作并完成我需要的一切,但有一个问题 - 当我在 Expression Blend 设计器中编辑网格内容时,子控件上的 Grid.Column 和 Grid.Row 属性会被重置。这很烦人。我可以做什么来检测 Blend 设计器的刷新并将这些附加属性重新应用到网格子项?

I have an attached property for a Grid. It is used to automatically lay out the content controls inside the Grid. What is basically does is that it goes through the Children collection and puts every control in the next free cell of the Grid control. It is done once (in the Initialized event handler).

    public static readonly DependencyProperty AutoLayoutProperty =
        DependencyProperty.RegisterAttached(
            "AutoLayout", 
            typeof(bool), 
            typeof(GridEx), 
            new UIPropertyMetadata(false, OnAutoLayoutChanged));

    private static void OnAutoLayoutChanged(
        DependencyObject d, 
        DependencyPropertyChangedEventArgs e)
    {
        var grid = d as Grid;
        if (!grid.IsInitialized) 
            grid.Initialized += new EventHandler(grid_Initialized);
        else { UpdateLayout(grid); }
    }
    private static void UpdateLayout(Grid grid)
    {
        foreach(var child in grid.Children) 
        {
            // Set Grid.Column and Grid.Row properties on the child
        }
    }

This code works and does everything I need, yet there is one problem - when I edit the contents of the grid in Expression Blend designer those Grid.Column and Grid.Row properties on child controls get reset. It is just annoying. What can I do to detect the refresh of the Blend designer and reapply those attached properties to grid children?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

樱娆 2024-12-06 00:06:58

请尝试使用 Loaded 事件。

编辑 - 为设计者添加了解决方法

private static void OnAutoLayoutChanged(
    DependencyObject d,
    DependencyPropertyChangedEventArgs e)
{
    var grid = d as Grid;            
    grid.Loaded += (object sender, RoutedEventArgs e2) =>
    {
        UpdateLayout(grid);
    };

    // Workaround for Blend..
    if (DesignerProperties.GetIsInDesignMode(grid) == true)
    {
        grid.LayoutUpdated += (object sender, EventArgs e2) =>
        {
            UpdateLayout(grid);
        };
    }
}

Try with the Loaded event instead.

Edit - Added workaround for designer

private static void OnAutoLayoutChanged(
    DependencyObject d,
    DependencyPropertyChangedEventArgs e)
{
    var grid = d as Grid;            
    grid.Loaded += (object sender, RoutedEventArgs e2) =>
    {
        UpdateLayout(grid);
    };

    // Workaround for Blend..
    if (DesignerProperties.GetIsInDesignMode(grid) == true)
    {
        grid.LayoutUpdated += (object sender, EventArgs e2) =>
        {
            UpdateLayout(grid);
        };
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文