Select to view content in your preferred language

Treeview Events not invoke MVVM methods

1564
1
05-07-2013 12:53 PM
MosesAsuquo
Deactivated User
Hi,

I have a usercontrol class that displays matchcandidates of a locatorTask in treeview after an address search has been performed by the user. The matchcandidates are displayed in the treeview but the treeview events are not firing or calling up the MVVM methods that are assigned to the event. Everything was working when i had everything in the codebehind of the xaml file. But i decided to switch to MVVM pattern and now the events are not firing for MVVM though they were working when i had methods in the code behind of the view.


Here is the view:

<UserControl x:Class="AddressLocator.AddIns.ResultView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:toolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"  
    xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
    xmlns:common="clr-namespace:System.Windows;assembly=System.Windows.Controls"
    xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"
    xmlns:local="clr-namespace:AddressLocator.AddIns"
    xmlns:controlsInputToolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Input.Toolkit"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">

    <UserControl.Resources>
        <local:VisibilityValueConverter x:Key="VisibilityValueConverter" />
        <Style TargetType="toolkit:TreeViewItem">
            <Setter Property="IsExpanded" Value="True" />
        </Style>

        <Style TargetType="Rectangle" x:Name="ZoomToBrush">
            <Setter Property="Fill">
                <Setter.Value>
                    <ImageBrush ImageSource="/AddressLocator.AddIns;component/Images/ZoomIn.png" />
                </Setter.Value>
            </Setter>
        </Style>

        <Style TargetType="Rectangle" x:Name="PanToBrush">
            <Setter Property="Fill">
                <Setter.Value>
                    <ImageBrush ImageSource="/AddressLocator.AddIns;component/Images/Pan.png" />
                </Setter.Value>
            </Setter>
        </Style>

        <Style TargetType="Rectangle" x:Name="RemoveBrush">
            <Setter Property="Fill">
                <Setter.Value>
                    <ImageBrush ImageSource="/AddressLocator.AddIns;component/Images/dismiss.png" />
                </Setter.Value>
            </Setter>
        </Style>
    </UserControl.Resources>

    <Grid x:Name="LayoutRoot" Background="White">
        <StackPanel Margin="0,0,0,0" Background="DarkGreen">
            <TextBlock Foreground="White" FontSize="12" Height="20" HorizontalAlignment="Center"  FontWeight="Bold"> Address Search Result</TextBlock>
        </StackPanel>
        <toolkit:TreeView x:Name="MyTreeView" Margin="0,20,0,0" BorderThickness="0,0,0,0" ItemsSource="{Binding Categories}" >
            <toolkit:TreeView.ItemTemplate >
                <common:HierarchicalDataTemplate ItemsSource="{Binding SubCategories}" >
                    <StackPanel Orientation="Horizontal">
                        <i:Interaction.Triggers>
                            <i:EventTrigger EventName="MouseRightButtonDown">
                                <ei:CallMethodAction MethodName="MouseRightButtonDown" TargetObject="{Binding}"/>
                            </i:EventTrigger>
                            <i:EventTrigger EventName="MouseRightButtonUp">
                                <ei:CallMethodAction MethodName="MouseRightButtonUp" TargetObject="{Binding}"/>
                            </i:EventTrigger>
                        </i:Interaction.Triggers>
                        
                        <CheckBox x:Name="chkStatus" Tag="{Binding self}" IsChecked= "{Binding IsSelected }" Margin="0,0,0,0" Visibility="{Binding hideChkBox,Converter={StaticResource VisibilityValueConverter},FallbackValue=Collapsed}">
                            <i:Interaction.Triggers>
                                <i:EventTrigger EventName="Click">
                                    <ei:CallMethodAction MethodName="chkStatus" TargetObject="{Binding}"/>
                                </i:EventTrigger>
                            </i:Interaction.Triggers>
                        </CheckBox>
                        <ContentControl Name="Address" Content="{Binding Address}" Margin="0,0,0,0">
                            <controlsInputToolkit:ContextMenuService.ContextMenu>
                                <controlsInputToolkit:ContextMenu Name="ContextMenuWindow" Visibility="{Binding hideChkBox,Converter={StaticResource VisibilityValueConverter},FallbackValue=Collapsed}">
                                    <controlsInputToolkit:MenuItem Header="Zoom To" Tag="{Binding self}" Visibility="{Binding hideContextMenu,Converter={StaticResource VisibilityValueConverter},FallbackValue=Collapsed}">
                                        <controlsInputToolkit:MenuItem.Icon>
                                            <Rectangle Width="16" Height="16" Style="{StaticResource ZoomToBrush}"/>
                                        </controlsInputToolkit:MenuItem.Icon>
                                        <i:Interaction.Triggers>
                                            <i:EventTrigger EventName="Click">
                                                <ei:CallMethodAction MethodName="ZoomTo" TargetObject="{Binding}"/>
                                            </i:EventTrigger>
                                        </i:Interaction.Triggers>
                                    </controlsInputToolkit:MenuItem>
                                    <controlsInputToolkit:MenuItem Header="Pan To" Tag="{Binding self}" Visibility="{Binding hideContextMenu,Converter={StaticResource VisibilityValueConverter},FallbackValue=Collapsed}">
                                        <controlsInputToolkit:MenuItem.Icon>
                                            <Rectangle Width="16" Height="16" Style="{StaticResource PanToBrush}"/>
                                        </controlsInputToolkit:MenuItem.Icon>
                                        <i:Interaction.Triggers>
                                            <i:EventTrigger EventName="Click">
                                                <ei:CallMethodAction MethodName="PanTo" TargetObject="{Binding}"/>
                                            </i:EventTrigger>
                                        </i:Interaction.Triggers>
                                    </controlsInputToolkit:MenuItem>
                                    <controlsInputToolkit:Separator/>
                                    <controlsInputToolkit:MenuItem Header="Remove" Tag="{Binding self}" >
                                        <controlsInputToolkit:MenuItem.Icon>
                                            <Rectangle Width="16" Height="16" Style="{StaticResource RemoveBrush}"/>
                                        </controlsInputToolkit:MenuItem.Icon>
                                        <i:Interaction.Triggers>
                                            <i:EventTrigger EventName="Click">
                                                <ei:CallMethodAction MethodName="Remove" TargetObject="{Binding}"/>
                                                <!-- Switch to measuring shapes -->
                                                <ei:ChangePropertyAction 
                                                TargetObject="{Binding ElementName=MyTreeView}" 
                                                PropertyName="ItemsSource" 
                                                    Value="null" />
                                                <ei:CallMethodAction
                                                TargetObject="{Binding ElementName=MyTreeView}" 
                                                MethodName="UpdateLayout"/>
                                                
                                                <ei:ChangePropertyAction 
                                                TargetObject="{Binding ElementName=MyTreeView}" 
                                                PropertyName="ItemsSource" 
                                                    Value="{Binding Categories}"/>
                                            </i:EventTrigger>
                                        </i:Interaction.Triggers>
                                    </controlsInputToolkit:MenuItem>
                                </controlsInputToolkit:ContextMenu>
                            </controlsInputToolkit:ContextMenuService.ContextMenu>
                        </ContentControl>
                    </StackPanel>
                </common:HierarchicalDataTemplate>
            </toolkit:TreeView.ItemTemplate>
        </toolkit:TreeView>
    </Grid>
</UserControl>


I have to make this work using MVVM. Please any helpful suggestion will be greatly appreciated. Thanks in advance.
0 Kudos
1 Reply
MosesAsuquo
Deactivated User
Hi,

I have a usercontrol class that displays matchcandidates of a locatorTask in treeview after an address search has been performed by the user. The matchcandidates are displayed in the treeview but the treeview events are not firing or calling up the MVVM methods that are assigned to the event. Everything was working when i had everything in the codebehind of the xaml file. But i decided to switch to MVVM pattern and now the events are not firing for MVVM though they were working when i had methods in the code behind of the view.


Here is the view:

<UserControl x:Class="AddressLocator.AddIns.ResultView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:toolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"  
    xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
    xmlns:common="clr-namespace:System.Windows;assembly=System.Windows.Controls"
    xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"
    xmlns:local="clr-namespace:AddressLocator.AddIns"
    xmlns:controlsInputToolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Input.Toolkit"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">

    <UserControl.Resources>
        <local:VisibilityValueConverter x:Key="VisibilityValueConverter" />
        <Style TargetType="toolkit:TreeViewItem">
            <Setter Property="IsExpanded" Value="True" />
        </Style>

        <Style TargetType="Rectangle" x:Name="ZoomToBrush">
            <Setter Property="Fill">
                <Setter.Value>
                    <ImageBrush ImageSource="/AddressLocator.AddIns;component/Images/ZoomIn.png" />
                </Setter.Value>
            </Setter>
        </Style>

        <Style TargetType="Rectangle" x:Name="PanToBrush">
            <Setter Property="Fill">
                <Setter.Value>
                    <ImageBrush ImageSource="/AddressLocator.AddIns;component/Images/Pan.png" />
                </Setter.Value>
            </Setter>
        </Style>

        <Style TargetType="Rectangle" x:Name="RemoveBrush">
            <Setter Property="Fill">
                <Setter.Value>
                    <ImageBrush ImageSource="/AddressLocator.AddIns;component/Images/dismiss.png" />
                </Setter.Value>
            </Setter>
        </Style>
    </UserControl.Resources>

    <Grid x:Name="LayoutRoot" Background="White">
        <StackPanel Margin="0,0,0,0" Background="DarkGreen">
            <TextBlock Foreground="White" FontSize="12" Height="20" HorizontalAlignment="Center"  FontWeight="Bold"> Address Search Result</TextBlock>
        </StackPanel>
        <toolkit:TreeView x:Name="MyTreeView" Margin="0,20,0,0" BorderThickness="0,0,0,0" ItemsSource="{Binding Categories}" >
            <toolkit:TreeView.ItemTemplate >
                <common:HierarchicalDataTemplate ItemsSource="{Binding SubCategories}" >
                    <StackPanel Orientation="Horizontal">
                        <i:Interaction.Triggers>
                            <i:EventTrigger EventName="MouseRightButtonDown">
                                <ei:CallMethodAction MethodName="MouseRightButtonDown" TargetObject="{Binding}"/>
                            </i:EventTrigger>
                            <i:EventTrigger EventName="MouseRightButtonUp">
                                <ei:CallMethodAction MethodName="MouseRightButtonUp" TargetObject="{Binding}"/>
                            </i:EventTrigger>
                        </i:Interaction.Triggers>
                        
                        <CheckBox x:Name="chkStatus" Tag="{Binding self}" IsChecked= "{Binding IsSelected }" Margin="0,0,0,0" Visibility="{Binding hideChkBox,Converter={StaticResource VisibilityValueConverter},FallbackValue=Collapsed}">
                            <i:Interaction.Triggers>
                                <i:EventTrigger EventName="Click">
                                    <ei:CallMethodAction MethodName="chkStatus" TargetObject="{Binding}"/>
                                </i:EventTrigger>
                            </i:Interaction.Triggers>
                        </CheckBox>
                        <ContentControl Name="Address" Content="{Binding Address}" Margin="0,0,0,0">
                            <controlsInputToolkit:ContextMenuService.ContextMenu>
                                <controlsInputToolkit:ContextMenu Name="ContextMenuWindow" Visibility="{Binding hideChkBox,Converter={StaticResource VisibilityValueConverter},FallbackValue=Collapsed}">
                                    <controlsInputToolkit:MenuItem Header="Zoom To" Tag="{Binding self}" Visibility="{Binding hideContextMenu,Converter={StaticResource VisibilityValueConverter},FallbackValue=Collapsed}">
                                        <controlsInputToolkit:MenuItem.Icon>
                                            <Rectangle Width="16" Height="16" Style="{StaticResource ZoomToBrush}"/>
                                        </controlsInputToolkit:MenuItem.Icon>
                                        <i:Interaction.Triggers>
                                            <i:EventTrigger EventName="Click">
                                                <ei:CallMethodAction MethodName="ZoomTo" TargetObject="{Binding}"/>
                                            </i:EventTrigger>
                                        </i:Interaction.Triggers>
                                    </controlsInputToolkit:MenuItem>
                                    <controlsInputToolkit:MenuItem Header="Pan To" Tag="{Binding self}" Visibility="{Binding hideContextMenu,Converter={StaticResource VisibilityValueConverter},FallbackValue=Collapsed}">
                                        <controlsInputToolkit:MenuItem.Icon>
                                            <Rectangle Width="16" Height="16" Style="{StaticResource PanToBrush}"/>
                                        </controlsInputToolkit:MenuItem.Icon>
                                        <i:Interaction.Triggers>
                                            <i:EventTrigger EventName="Click">
                                                <ei:CallMethodAction MethodName="PanTo" TargetObject="{Binding}"/>
                                            </i:EventTrigger>
                                        </i:Interaction.Triggers>
                                    </controlsInputToolkit:MenuItem>
                                    <controlsInputToolkit:Separator/>
                                    <controlsInputToolkit:MenuItem Header="Remove" Tag="{Binding self}" >
                                        <controlsInputToolkit:MenuItem.Icon>
                                            <Rectangle Width="16" Height="16" Style="{StaticResource RemoveBrush}"/>
                                        </controlsInputToolkit:MenuItem.Icon>
                                        <i:Interaction.Triggers>
                                            <i:EventTrigger EventName="Click">
                                                <ei:CallMethodAction MethodName="Remove" TargetObject="{Binding}"/>
                                                <!-- Switch to measuring shapes -->
                                                <ei:ChangePropertyAction 
                                                TargetObject="{Binding ElementName=MyTreeView}" 
                                                PropertyName="ItemsSource" 
                                                    Value="null" />
                                                <ei:CallMethodAction
                                                TargetObject="{Binding ElementName=MyTreeView}" 
                                                MethodName="UpdateLayout"/>
                                                
                                                <ei:ChangePropertyAction 
                                                TargetObject="{Binding ElementName=MyTreeView}" 
                                                PropertyName="ItemsSource" 
                                                    Value="{Binding Categories}"/>
                                            </i:EventTrigger>
                                        </i:Interaction.Triggers>
                                    </controlsInputToolkit:MenuItem>
                                </controlsInputToolkit:ContextMenu>
                            </controlsInputToolkit:ContextMenuService.ContextMenu>
                        </ContentControl>
                    </StackPanel>
                </common:HierarchicalDataTemplate>
            </toolkit:TreeView.ItemTemplate>
        </toolkit:TreeView>
    </Grid>
</UserControl>


I have to make this work using MVVM. Please any helpful suggestion will be greatly appreciated. Thanks in advance.



I have solved this problem. Thanks.
0 Kudos