[英]Accessing children of UserControl at design time
Windows 8样式应用程序(例如“ Metro”),Visual Studio 2012,XAML。
我有一个从Canvas派生的UserControl。 它有一个子元素-一个多边形,其数据绑定到一个属性(实现了INotifyPropertyChanged):
<Canvas x:Name="MyPolygon">
<Polygon Points="{Binding ElementName=MyPolygon,Path=MyPoints}" ... />
</Canvas>
如果我在XAML中的其他地方实例化了该控件,并传入了一个字符串,则会设置该属性并在设计时和运行时正确渲染Polygon:
<local:MyPolygon MyPoints="..." />
但是,更改该字符串中的值很麻烦。 设计人员希望有一些UI结(如Ellipses)在设计时可见,而在运行时不可见的集合,以便他们可以将其拖到设计器中,并让Polygon即时重建其几何形状:
<local:MyPolygon>
<Ellipse Canvas.Left="204" Canvas.Top="57" ... />
<Ellipse Canvas.Left="166" Canvas.Top="30" ... />
...
</local:MyPolygon>
基本上,我想将几何信息保留在(扩展).Children中。 这可能吗?
(可能有一些事件/构造函数,控件可以检查其.Children(在插入了椭圆之后),检索其坐标并构建MyPoints。设计人员将必须触发该事件,以便几何在设计时可见。
你有没有看设计数据像这样 。
if (Windows.ApplicationModel.DesignMode.DesignModeEnabled)
{
GetSampleData();
}
else GetRealData();
要么
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
<CollectionViewSource
x:Name="groupedItemsViewSource"
Source="{Binding Groups}"
IsSourceGrouped="true"
ItemsPath="Items"
d:Source="{Binding ItemGroups,
Source={d:DesignInstance Type=data:SampleDataSource,
IsDesignTimeCreatable=True}}"/>
因此,我最终在与椭圆相同的级别上创建了多边形。
<Polygon Points="{Binding ElementName=MyPoints,Converter={StaticResource PolygonConverter}}" ... />
<Canvas x:Name="MyPoins">
<Ellipse Canvas.Left="228" Canvas.Top="69" ... />
<Ellipse Canvas.Left="166" Canvas.Top="30" ... />
...
</Canvas>
绑定转换器将对象的所有.Children的坐标转换为字符串。
这在设计时和运行时均有效。
不幸的是,为了让VS设计人员刷新视图并获取更改,必须在移动Ellipes之后重新构建项目,这使设计过程比以前想象的要直观得多。 :/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.