1

So my problem at a glance is that I want the right mouse button of my TreeView to select an item. I'm using WPF with XAML and C#.

That's not too hard, you might think - just attach a right click event to the treeview, cast the e.Source as a TreeViewItem and then play with the 'IsSelected' property.

And it would be that simple, but I'm using the MVVM approach so e.Source and things like that come back with information from my view model.

I'll provide the XAML:

<TreeView x:Name="ScenesTreeView01" Grid.Column="0" Width="Auto" Background="AliceBlue" ItemsSource="{Binding Scenes}" SelectedItemChanged="TreeView_SelectedItemChanged" BorderThickness="0">
        <TreeView.DataContext>
            <viewModels:ScenesViewModel />
        </TreeView.DataContext>
        <TreeView.Resources>
            <ContextMenu x:Key="SceneLevel">
                            <MenuItem Header="Add selected character" Command="{Binding Path=DataContext.AddSelectedCharacter, Source={x:Reference ScenesTreeView01}}"/>
            </ContextMenu>
            <ContextMenu x:Key="CharacterLevel">
                            <MenuItem Header="Remove character from scene" Command="{Binding Path=DataContext.RemoveCharacterFromScene, Source={x:Reference ScenesTreeView01}}"/>
            </ContextMenu>

                    </TreeView.Resources>
        <TreeView.ItemContainerStyle>
            <Style TargetType="{x:Type TreeViewItem}">
                <Setter Property="BorderThickness" Value="1.5"/>
                <Style.Resources>
                    <Style TargetType="Border">
                        <Setter Property="CornerRadius" Value="5"/>
                    </Style>
                </Style.Resources>
            </Style>
        </TreeView.ItemContainerStyle>
        <TreeView.ItemTemplate>
            <HierarchicalDataTemplate ItemsSource="{Binding Characters}">
                    <StackPanel Orientation="Horizontal" ContextMenu="{StaticResource SceneLevel}">
                    <TextBlock Text="{Binding SceneName}"></TextBlock>

                        <Image Source="{StaticResource ImgBook1}" Margin="0,0,5,0" Width="32" Height="32"/>

                </StackPanel>
                <HierarchicalDataTemplate.ItemTemplate>
                    <DataTemplate>
                        <Border BorderThickness="2" BorderBrush="LightBlue" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="2" CornerRadius="5,5,5,5">
                            <StackPanel Orientation="Horizontal" Margin="3" ContextMenu="{StaticResource CharacterLevel}">

                            <TextBlock FontFamily="Levenim MT" FontSize="16" VerticalAlignment="Center" MinWidth="50" Text="{Binding FirstName}"></TextBlock>

                                <Image Source="{Binding ImgIcon}" Margin="2" Width="32" Height="32"/>

                        </StackPanel>
                        </Border>
                    </DataTemplate>
                </HierarchicalDataTemplate.ItemTemplate>
            </HierarchicalDataTemplate>
        </TreeView.ItemTemplate>
    </TreeView>

So I'm a little bit lost on this one. My aim is to have the right click for context menu also select an item (like in Windows Explorer).

Thanks for reading.

Guilherme Fidelis
  • 1,022
  • 11
  • 20
  • Why are you asking the same question again?: http://stackoverflow.com/questions/43045718/wpf-selection-of-a-treeview-item-as-context-menu-is-called – mm8 Mar 30 '17 at 11:56
  • I tried all the solutions and none worked, and now the question is too old for people to look at. I don't know a way around this problem aside to reformulate the question and ask again, including some information that might narrow down the issue (which is what I have done). If there is a way around it, please let me know. – TheFaithfulLearner Mar 30 '17 at 12:00
  • 2
    Possible duplicate of [WPF selection of a TreeView item as context menu is called](http://stackoverflow.com/questions/43045718/wpf-selection-of-a-treeview-item-as-context-menu-is-called) – mm8 Mar 30 '17 at 12:01
  • I have already suggested a solution to your issue. – mm8 Mar 30 '17 at 12:02
  • And very grateful I am too, but it didn't work so that leaves me up the creek. – TheFaithfulLearner Mar 30 '17 at 12:04
  • To elaborate, I added the style as suggested in your answer and then added the method to the code behind for that user control. The method didn't fire, and so that's where I was. – TheFaithfulLearner Mar 30 '17 at 12:06
  • 2
    The solution posted by mm8 is the correct way to solve this. People still look at older questions, posting duplicates is bad practice. 'The method didn't fire, and so that's where I was' - This is not enough information for anybody assist you. Asking the same question again does not help in solving the issue, just annoys people. – Alex Mar 30 '17 at 12:37
  • @TheFaithfulLearner: I have posted an answer to the duplicate, I hope this will help you. – Rekshino Mar 30 '17 at 12:55
  • @Alex Well I'm sure you've been on enough forums in your time, as have I, and I know (and I imagine you do too) that the key factor is time. Nobody's looking at that three day old question. Maybe if you're lucky, but the odds go down quickly. And sometimes you read the question you wrote and think perhaps you can do a better job. You could edit the one you originally wrote, but that doesn't beat the problem of time. If you find it annoying... well. – TheFaithfulLearner Mar 30 '17 at 16:21
  • @TheFaithfulLearner if you ever accidentially answer a year old question because someone decided to do a minor beauty edit and you didn't bother to check the original date, you will know that at stackoverflow, a well done question edit is enough to re-gain attention to a question. Please don't spam multiple questions in the future. – grek40 Apr 07 '17 at 15:46

0 Answers0