同步视图模型和视图
我有一个由一些节点和一些连接器组成的视图模型:
public class ViewModel
{
public List<Node> Nodes{get;set;}
public List<Connector> Connectors{get;set;}
}
public Class Node
{
public Point Position{get;set;}
}
public class Connector
{
public Node StartNode{get;set;}
public Node EndNode{get;set;}
}
现在我通过项目控件在页面中显示节点:
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Styles.xaml"></ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
<local:ElementThumbConverter x:Key="ElementThumbConverter"/>
<local:ElementThumbConverter2 x:Key="ElementThumbConverter2"/>
<Style x:Key="ElementThumbVMDataTemplateStyle" TargetType="ContentPresenter">
<Setter Property="Canvas.Left" Value="{Binding CanvasLeft,Mode=TwoWay}" />
<Setter Property="Canvas.Top" Value="{Binding CanvasTop,Mode=TwoWay}" />
<!--<Setter Property="Canvas.Top" Value="{Binding RelativeSource={RelativeSource Self},Path=ContentTemplate.elementThumb.(Canvas.Top), Mode=TwoWay}" />-->
</Style >
<DataTemplate DataType="{x:Type vm:ElementThumbVM}">
<!--Canvas.Left="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ContentPresenter},Path=(Canvas.Left),Mode=TwoWay}"-->
<kw:ElementThumb Name="elementThumb" Tag=""
Canvas.Left="{Binding CanvasLeft,Mode=TwoWay}"
Canvas.Top="{Binding CanvasTop,Mode=TwoWay}">
</kw:ElementThumb>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding ElementType}">
<DataTrigger.Value>
<vm:ElementType>BusinessServer</vm:ElementType>
</DataTrigger.Value>
<Setter TargetName="elementThumb" Property="Style" Value="{StaticResource BussinesServerStyle}"/>
</DataTrigger>
<DataTrigger Binding="{Binding ElementType}">
<DataTrigger.Value>
<vm:ElementType>DataBase</vm:ElementType>
</DataTrigger.Value>
<Setter TargetName="elementThumb" Property="Style" Value="{StaticResource DataBaseStyle}"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
<DataTemplate DataType="{x:Type vm:ConnectorVM}">
<kw:Connector>
<kw:Connector.StartElementThumb>
<Binding Converter="{StaticResource ElementThumbConverter2}">
<Binding.Source>
<MultiBinding Converter="{StaticResource ElementThumbConverter}">
<Binding Path="ElementThumbVMStartId" />
<Binding RelativeSource="{RelativeSource Mode=FindAncestor,AncestorType=kw:DiagramCanvas}"/>
</MultiBinding>
</Binding.Source>
</Binding>
</kw:Connector.StartElementThumb>
<kw:Connector.EndElementThumb>
<Binding Converter="{StaticResource ElementThumbConverter2}">
<Binding.Source>
<MultiBinding Converter="{StaticResource ElementThumbConverter}">
<Binding Path="ElementThumbVMEndId" />
<Binding RelativeSource="{RelativeSource Mode=FindAncestor,AncestorType=kw:DiagramCanvas}"/>
</MultiBinding>
</Binding.Source>
</Binding>
</kw:Connector.EndElementThumb>
</kw:Connector>
</DataTemplate>
</ResourceDictionary>
</UserControl.Resources>
<ItemsControl HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
ItemsSource="{Binding AllElements}" x:Name="mainCanvasControl"
ItemContainerStyle="{StaticResource ElementThumbVMDataTemplateStyle}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<kw:DiagramCanvas Name="mainCanvas" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
如您所见,我已经显示了节点和连接器的模板,它对于节点来说工作得很好, 但对于连接器,我需要获取其相应的开始和结束节点,以便当 UI 中的节点更改其位置时,我可以跟踪其变化并将其反映在连接器中,我还需要最终显示显示节点的高度和高度(我说 ElmenetThumb)。 简而言之,模型中的每个节点在页面中都有一个类似的 ElmenetThumb,我想将起始节点和结束节点的类似 ElmenetThumb 传递到连接器的类似元素以进行位置跟踪。我尝试通过连接器执行此操作,如上面的代码所示,但它不起作用,因为在调用 ItemsControl 的转换内容时,未完全构建。
I have a view model that consist of some nodes and some connectors:
public class ViewModel
{
public List<Node> Nodes{get;set;}
public List<Connector> Connectors{get;set;}
}
public Class Node
{
public Point Position{get;set;}
}
public class Connector
{
public Node StartNode{get;set;}
public Node EndNode{get;set;}
}
Now I display nodes in page by an items control:
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Styles.xaml"></ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
<local:ElementThumbConverter x:Key="ElementThumbConverter"/>
<local:ElementThumbConverter2 x:Key="ElementThumbConverter2"/>
<Style x:Key="ElementThumbVMDataTemplateStyle" TargetType="ContentPresenter">
<Setter Property="Canvas.Left" Value="{Binding CanvasLeft,Mode=TwoWay}" />
<Setter Property="Canvas.Top" Value="{Binding CanvasTop,Mode=TwoWay}" />
<!--<Setter Property="Canvas.Top" Value="{Binding RelativeSource={RelativeSource Self},Path=ContentTemplate.elementThumb.(Canvas.Top), Mode=TwoWay}" />-->
</Style >
<DataTemplate DataType="{x:Type vm:ElementThumbVM}">
<!--Canvas.Left="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ContentPresenter},Path=(Canvas.Left),Mode=TwoWay}"-->
<kw:ElementThumb Name="elementThumb" Tag=""
Canvas.Left="{Binding CanvasLeft,Mode=TwoWay}"
Canvas.Top="{Binding CanvasTop,Mode=TwoWay}">
</kw:ElementThumb>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding ElementType}">
<DataTrigger.Value>
<vm:ElementType>BusinessServer</vm:ElementType>
</DataTrigger.Value>
<Setter TargetName="elementThumb" Property="Style" Value="{StaticResource BussinesServerStyle}"/>
</DataTrigger>
<DataTrigger Binding="{Binding ElementType}">
<DataTrigger.Value>
<vm:ElementType>DataBase</vm:ElementType>
</DataTrigger.Value>
<Setter TargetName="elementThumb" Property="Style" Value="{StaticResource DataBaseStyle}"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
<DataTemplate DataType="{x:Type vm:ConnectorVM}">
<kw:Connector>
<kw:Connector.StartElementThumb>
<Binding Converter="{StaticResource ElementThumbConverter2}">
<Binding.Source>
<MultiBinding Converter="{StaticResource ElementThumbConverter}">
<Binding Path="ElementThumbVMStartId" />
<Binding RelativeSource="{RelativeSource Mode=FindAncestor,AncestorType=kw:DiagramCanvas}"/>
</MultiBinding>
</Binding.Source>
</Binding>
</kw:Connector.StartElementThumb>
<kw:Connector.EndElementThumb>
<Binding Converter="{StaticResource ElementThumbConverter2}">
<Binding.Source>
<MultiBinding Converter="{StaticResource ElementThumbConverter}">
<Binding Path="ElementThumbVMEndId" />
<Binding RelativeSource="{RelativeSource Mode=FindAncestor,AncestorType=kw:DiagramCanvas}"/>
</MultiBinding>
</Binding.Source>
</Binding>
</kw:Connector.EndElementThumb>
</kw:Connector>
</DataTemplate>
</ResourceDictionary>
</UserControl.Resources>
<ItemsControl HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
ItemsSource="{Binding AllElements}" x:Name="mainCanvasControl"
ItemContainerStyle="{StaticResource ElementThumbVMDataTemplateStyle}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<kw:DiagramCanvas Name="mainCanvas" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
As you see I have displaying template for nodes and connector, it work fine for nodes,
but for connector I need to gets its corresponding start and end node so when a node in UI change its position I can track its change and reflects it in connector, I also need finally displaying with and height of displaying node (I say ElmenetThumb).
In short, each node in model has a analogous ElmenetThumb in page I want to pass analogous ElmenetThumb of start and end nodes to analogous element of connector for position tracking. I trying to do that by connector as you can see in above code, but it doesn't work because in the time of calling convert content of ItemsControl not built completely.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我以另一种完全不同的方式做到这一点,我不能说它是 ViewModel。 A 为我的画布定义显示方法:
但我认为这不是最好的方法。
你怎么认为?
I do that in another completely different way, that I can't say it ViewModel. A Define a display method for my canvas :
But I think this isn't the best approach.
What do you think?