在设计器视图中重置附加属性
我有一个网格的附加属性。它用于自动布局网格内的内容控件。它的基本作用是遍历 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请尝试使用
Loaded
事件。编辑 - 为设计者添加了解决方法
Try with the
Loaded
event instead.Edit - Added workaround for designer