【发布时间】:2011-01-15 10:49:42
【问题描述】:
第三次尝试描述问题:
- 尝试 1: Sunchronizing view model and view
- 尝试2: WPF ViewModel not active presenter
- 尝试3:
我有一些视图模型类:
public class Node : INotifyPropertyChanged
{
Guid NodeId { get; set; }
public string Name { get; set; }
}
public class Connection: INotifyPropertyChanged
{
public Node StartNode { get; set; }
public Node EndNode { get; set; }
}
public class SettingsPackModel
{
public List<Node> Nodes { get; private set; }
public List<Connection> Connections { get; private set; }
}
我也有一些模板来展示这些模型:
<DataTemplate DataType="{x:Type vm:Node}">…</DataTemplate>
<DataTemplate DataType="{x:Type vm:Connection}">
<my:ConnectionElment StartNodeElment="???" EndNodeElment="???">
</my:ConnectionElment>
<DataTemplate>
但问题是连接的 DataTemplate 需要引用两个 UIElement 类型的元素,我怎样才能通过这两个,我该如何填写???在上面的表达式中?
编辑: 我实际上想隐藏这次尝试的一部分,但正如我在那里描述的那样:Sunchronizing view model and view。我会使用这样的东西:
<ItemsControl ItemsSource="{Binding AllElements}"
ItemContainerStyle="{StaticResource ElementThumbVMDataTemplateStyle>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<kw:DiagramCanvas />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
<Style x:Key="ElementThumbVMDataTemplateStyle" TargetType="ContentPresenter">
<Setter Property="Canvas.Left" Value="{Binding CanvasLeft,Mode=TwoWay}" />
<Setter Property="Canvas.Top" Value="{Binding CanvasTop,Mode=TwoWay}" /> </Style >
Node DataTemplate 类似这样:
<DataTemplate DataType="{x:Type vm:Node}">
<kw:ElementThumb Canvas.Left="{Binding CanvasLeft,Mode=TwoWay}"
Canvas.Top="{Binding CanvasTop,Mode=TwoWay}">
</kw:ElementThumb>
</DataTemplate>
Canvasleft 和 CanvasTop 是存在于 Node 和 ElementThumb 类中的属性。
【问题讨论】:
-
越位:
INotifyPropertyChanged和 auto-imp 属性是一个奇怪的组合。 -
我会在需要时立即实施,但这里的问题是另一回事。