In WPF,MVVM should ViewModel be involved in opening a window, e.g. About-window?

南笙酒味 提交于 2020-01-24 17:08:05

问题


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>

回答1:


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?




回答2:


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.



来源:https://stackoverflow.com/questions/4484430/in-wpf-mvvm-should-viewmodel-be-involved-in-opening-a-window-e-g-about-window

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!