ViewModel 视图关系/链接/同步

发布于 2024-10-12 10:53:38 字数 2482 浏览 3 评论 0原文

第三次尝试描述问题:

我有一些视图模型类:

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 for Connection 需要引用 UIElement 类型的两个元素,我如何传递这两个元素,我如何填充???在上面的表达式中?

编辑: 我实际上想隐藏此尝试中的一部分,但正如我在那里描述的那样: Sunchronizing 视图模型和查看。我会使用这样的东西:

<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 类中存在的属性。

Third try to describing problem:

I have some class for view models:

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; }
}

I also have some templates to displays these models:

<DataTemplate DataType="{x:Type vm:Node}">…</DataTemplate>
<DataTemplate DataType="{x:Type vm:Connection}">
 <my:ConnectionElment StartNodeElment="???" EndNodeElment="???"> 
</my:ConnectionElment>
<DataTemplate>

But the problem is that DataTemplate for Connection need reference ot two element of type UIElement , how can I pass these two, how can I fill ??? in above expression?

Edit:
I actually want to hide that's part in this try, but as I describe it there: Sunchronizing view model and view. I would use something like this :

<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 > 

And something like this for 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 and CanvasTop are properties that exist in Node and also ElementThumb classes.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千と千尋 2024-10-19 10:53:38

将要为其创建 DataTemplate 的类型的对象放置在 DataTemplate 本身内部是毫无意义的。数据模板用于创建数据的可视化表示,因此您首先需要了解如何可视化节点和连接。

Placing an object of the type you are creating a DataTemplate for inside the DataTemplate itself is quite pointless. DataTemplates are there for the creation of a visual representation of your data, so you first need a concept of how you want to visualize your Nodes and Connections.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文