2

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>
Anders Juul
  • 2,407
  • 3
  • 34
  • 56

2 Answers2

2

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?

H H
  • 263,252
  • 30
  • 330
  • 514
  • Sorry - I got two questions mixed up there! – Anders Juul Dec 20 '10 at 07:02
  • As for the question in the 'body': I have viewmodel-objects handling the individual treeitems with regards to display, lazy load etc. However, when I want to 'activate'/double-click an item in the treeview, I'd like this action to be handled by the same viewmodel objects that handle the display - but how do I do that? – Anders Juul Dec 20 '10 at 07:05
  • I marked this as answered as the title question got answered. The 'body' question has been reposted [here](http://stackoverflow.com/questions/4497825/wpf-mvvm-how-to-handle-double-click-on-treeviewitems-in-the-viewmodel). Sorry for any inconveneince (hard word ;-) ) – Anders Juul Dec 21 '10 at 09:32
  • Hi Anders, I guess this might end up confusing people. Why not edit the question to describe the original issue - you reposted the ther question, right? Cheers.... – Sebastian Edelmeier Jun 01 '11 at 05:48
0

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.

  • 1
    Hi Torben, thanks for adding your comments! I think I have a satisfactory solution now even though mapping is done through string literals in XAML as you point out. I do think that the added complexity from mapping via some fancy lambda expression would not be worth it. Have we had this discussion before ;-) ? Still - thanks, see you soon. – Anders Juul Feb 09 '11 at 11:48