在WPF中,MVVM是否应该让ViewModel参与打开窗口,例如About-window?

发布于 2024-10-08 08:13:05 字数 2515 浏览 0 评论 0原文

我有一个标准的 WPF 树视图,并绑定了查看模型类的项目。

我现在希望处理双击项目时的行为(打开视觉工作室风格的文档)。

我可以让事件处理程序在包含树视图的控件中触发(显示的 xaml),但是如何绑定到视图模型类上的特定行为 - 例如 ProjectViewModel?

最好绑定到 ICommand-implementer,因为这在其他地方使用...

感谢您的任何评论,

Anders,丹麦

    <TreeView ItemsSource="{Binding Projects}" MouseDoubleClick="TreeView_MouseDoubleClick">
        <TreeView.ItemContainerStyle>
            <!-- 
    This Style binds a TreeViewItem to a TreeViewItemViewModel. 
    -->
            <Style TargetType="{x:Type TreeViewItem}">
                <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
                <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
                <Setter Property="FontWeight" Value="Normal" />
                <Style.Triggers>
                    <Trigger Property="IsSelected" Value="True">
                        <Setter Property="FontWeight" Value="Bold" />
                    </Trigger>
                </Style.Triggers>
            </Style>
        </TreeView.ItemContainerStyle>

        <TreeView.Resources>
            <HierarchicalDataTemplate DataType="{x:Type Implementations:ProjectViewModel}" ItemsSource="{Binding Children}">
                <StackPanel Orientation="Horizontal">
                    <Image Width="16" Height="16" Margin="3,0" Source="Images\Region.png" />
                    <TextBlock Text="{Binding DisplayName}" />
                </StackPanel>
            </HierarchicalDataTemplate>

            <HierarchicalDataTemplate DataType="{x:Type Implementations:PumpViewModel}" ItemsSource="{Binding Children}">
                <StackPanel Orientation="Horizontal">
                    <Image Width="16" Height="16" Margin="3,0" Source="Images\State.png" />
                    <TextBlock Text="{Binding Name}" />
                </StackPanel>
            </HierarchicalDataTemplate>

            <DataTemplate DataType="{x:Type Implementations:PumpDesignViewModel}">
                <StackPanel Orientation="Horizontal">
                    <Image Width="16" Height="16" Margin="3,0" Source="Images\City.png" />
                    <TextBlock Text="{Binding Name}" />
                </StackPanel>
            </DataTemplate>
        </TreeView.Resources>
    </TreeView>

I have a standard WPF treeview and have bound items to view model classes.

I now wish to handle behaviour when items are double-clicked (opening documents visual-studio-style).

I can get event-handler to fire in the control housing the treeview (xaml shown), but how do I bind to specific behaviour on the view model classes - e.g. ProjectViewModel?

Preferable bound to ICommand-implementer, as this is used elsewhere...

Thanks for any comments,

Anders, Denmark

    <TreeView ItemsSource="{Binding Projects}" MouseDoubleClick="TreeView_MouseDoubleClick">
        <TreeView.ItemContainerStyle>
            <!-- 
    This Style binds a TreeViewItem to a TreeViewItemViewModel. 
    -->
            <Style TargetType="{x:Type TreeViewItem}">
                <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
                <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
                <Setter Property="FontWeight" Value="Normal" />
                <Style.Triggers>
                    <Trigger Property="IsSelected" Value="True">
                        <Setter Property="FontWeight" Value="Bold" />
                    </Trigger>
                </Style.Triggers>
            </Style>
        </TreeView.ItemContainerStyle>

        <TreeView.Resources>
            <HierarchicalDataTemplate DataType="{x:Type Implementations:ProjectViewModel}" ItemsSource="{Binding Children}">
                <StackPanel Orientation="Horizontal">
                    <Image Width="16" Height="16" Margin="3,0" Source="Images\Region.png" />
                    <TextBlock Text="{Binding DisplayName}" />
                </StackPanel>
            </HierarchicalDataTemplate>

            <HierarchicalDataTemplate DataType="{x:Type Implementations:PumpViewModel}" ItemsSource="{Binding Children}">
                <StackPanel Orientation="Horizontal">
                    <Image Width="16" Height="16" Margin="3,0" Source="Images\State.png" />
                    <TextBlock Text="{Binding Name}" />
                </StackPanel>
            </HierarchicalDataTemplate>

            <DataTemplate DataType="{x:Type Implementations:PumpDesignViewModel}">
                <StackPanel Orientation="Horizontal">
                    <Image Width="16" Height="16" Margin="3,0" Source="Images\City.png" />
                    <TextBlock Text="{Binding Name}" />
                </StackPanel>
            </DataTemplate>
        </TreeView.Resources>
    </TreeView>

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

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

发布评论

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

评论(2

风轻花落早 2024-10-15 08:13:05

对于标题问题:是的。主视图的虚拟机应显示 AboutBox。

但消息正文似乎有所不同,也许您可​​以扩展一下?

To the Title-question: Yes. The VM of the Main View should show the AboutBox.

But the body of the message seems different, maybe you can expand on it a little?

清风疏影 2024-10-15 08:13:05

执行此操作的首选方法是使用您已经提到的命令模式。
即通过依赖属性绑定到 ICommand 实现。

依赖属性实际上是由静态支持属性实现的,该属性实现了框架使用的依赖内容。

不幸的是,MS 决定实现支持属性的方式至少可以说不是最佳的。

它通过硬编码字符串连接到您在 xaml 中绑定的公共非静态属性...

我不记得在哪里,但我发现了一个非常优雅的静态/非静态关系解决方案,它使用一个 lambda 表达式来进行映射,从而完全消除了硬编码、容易出错的字符串映射。

如果您仍然需要它,我可以向您发送更多有关此的信息。您知道在哪里联系我:-)

您需要解决的另一个问题是抽象视图的实现以保持可测试性。

The preferred way to do this is to use the command pattern as you already mentionend.
I.e. to bind to an ICommand implementation via a dependency property.

Dependency properties are actually implemented by a static backing property which implements the dependency stuff used by the framework.

Unfortunately the way MS decided to implement the backing property is - well, not optimal to say the least.

It is hooked up to the public, non-static property you bind to in xaml - by means of a hardcoded string...

I don't remember where but I found a quite elegant solution to the static/non-static relationship which uses a lambda expression to do the mapping thus completely removing the hardcoded, error prone string mapping.

If you still need it I can send you some more info on this. You know where to reach me :-)

Another concern you will need to adress is to abstract away the implementation of the view to maintain testability.

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