【发布时间】:2011-09-28 05:53:26
【问题描述】:
也许这是一个简单的问题,但我找不到答案。 我有三个仅颜色不同的用户控件。其中有一个代码:
<UserControl x:Class="SilverlightApplication14.NodePicture"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:SilverlightApplication14">
<UserControl.Resources>
<local:NodeViewModel x:Key="Children" />
</UserControl.Resources>
<Grid x:Name="LayoutRootNodePicture" Height="100" Width="100"
HorizontalAlignment="Center" DataContext="{Binding Source={StaticResource Children}, Path=Children}" >
<Canvas x:Name="ParentCanvas" Background="White" Width="100" Height="100" >
<Rectangle Fill="Yellow" Stroke="Blue" Width="100" Height="100" >
</Rectangle >
</Canvas>
<Image HorizontalAlignment="Center"
Source="add.png"
Stretch="Fill"
Width="16"
VerticalAlignment="Top"
Margin="0,0,2,2"
Height="16" MouseLeftButtonDown="Image_MouseLeftButtonDown">
</Image>
</Grid>
</UserControl>
如何将它们组合成 ObservableCollection Children?
public class NodeViewModel : INotifyPropertyChanged
{
public ObservableCollection<NodeViewModel> Children
{
get { return _children; }
set
{
_children = value;
NotifyChange("Children");
}
}
private void NotifyChange(string propName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propName));
}
}
我怎样才能使用这个控件集合的元素?
有没有一种简单(或正确)的方式来做到这一点?
【问题讨论】:
-
不清楚你在追求什么。您说您在上面设置了三个用户控件
DataContext="{Binding Source={StaticResource Children}, Path=Children}",并且您想将这三个组合到Chilren集合中? “控件集合”是什么意思? -
是的,我想将这三个组合到儿童系列 @bitbonk
-
你不能因为它是
NodeViewModel实例的集合而不是UserControl实例的集合。 -
在决定如何实现此控件之前,我需要知道以下两个问题的答案:1) 可以向视图模型添加其他属性吗? 2) 这三个用户控件在 xaml 代码中有何不同?如果将它们声明为
<local:NodePicture NodePictureColor="Yellow" />之类的名称会更容易。 -
1) 是的,您可以添加其他属性。 2)正如我在本页顶部所说:“它们仅在颜色上有所不同”。 @漩涡
标签: c# asp.net silverlight silverlight-4.0 silverlight-3.0