简体   繁体   中英

How to get selected Item from listbox to context menu

So I know there are similar questions out there but I'm still confused and hope someone can help me. i had a listbox with the ability to select an item and it would add a property. I now have a context menu as the layout is exactly what I need but I'm struggling to make my click event work, currently I can't do anything with mine just now, but I have the code for the code behind on the list box to work, can someone help me with having the lsitbox click event work on the context menu please :)?

private void Selector_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        var listBox = sender as ListBox;
        if (listBox.SelectedItems.Count == 0)
        {
            return;
        }
        var item = listBox.SelectedItems[0] as PropertyNode;
        viewModel.AddPropertyNode(item);
    }

Edit

Here is my XAML that I have with my context menu just now

 <DataTemplate x:Key="AddNodeTemplate">
        <Border BorderThickness="1" Background="#F7F7F7">
            <Border.BorderBrush>
                <DrawingBrush Viewport="8,8,8,8" ViewportUnits="Absolute" TileMode="Tile">
                    <DrawingBrush.Drawing>
                        <DrawingGroup>
                            <GeometryDrawing Brush="#F7F7F7">
                                <GeometryDrawing.Geometry>
                                    <GeometryGroup>
                                        <RectangleGeometry Rect="0,0,50,50"/>
                                        <RectangleGeometry Rect="50,50,50,50"/>
                                    </GeometryGroup>
                                </GeometryDrawing.Geometry>
                            </GeometryDrawing>
                        </DrawingGroup>
                    </DrawingBrush.Drawing>
                </DrawingBrush>
            </Border.BorderBrush>
            <StackPanel>
                <Button x:Name="ButtonAdd" Click="ButtonAdd_Click" Height="30" Width="130" Tag="{Binding RelativeSource={RelativeSource AncestorType={x:Type syncfusion:Node}}}">
                    <Button.Style>
                        <Style TargetType="Button">
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate TargetType="Button">
                                        <Grid Background="#F7F7F7">
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="25"/>
                                                <ColumnDefinition/>
                                            </Grid.ColumnDefinitions>
                                            <Image Source="Images/icon_plus.bmp" HorizontalAlignment="Left" Margin="5,0,0,0"/>
                                            <TextBlock Text="Add Property" HorizontalAlignment="Center" Grid.Column="1" VerticalAlignment="Center" Foreground="LightGray" FontStyle="Italic" FontSize="12"/>
                                        </Grid>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                            <Setter Property="Background" Value="#F7F7F7"/>
                        </Style>
                    </Button.Style>
                    <Button.ContextMenu>
                        <ContextMenu>
                            <MenuItem Header="_Copy Existing" Icon="{StaticResource ImageCopy}" ItemsSource="{Binding Path=AvailableProperties}" Click="MenuItem_Click">
                                <MenuItem.Resources>
                                    <Style TargetType="MenuItem">
                                        <Style.Resources>
                                            <Image x:Key="img" x:Shared="False" Width="12" Height="12" Source="{Binding Icon, Converter={StaticResource ImageToSourceConverter}}" 
                                                   Margin="3" VerticalAlignment="Center"/>
                                            <Style TargetType="ContentPresenter">
                                                <Style.Triggers>
                                                    <Trigger Property="ContentSource" Value="Icon">
                                                        <Setter Property="ContentTemplate">
                                                            <Setter.Value>
                                                                <DataTemplate>
                                                                    <Image Source="{Binding}"/>
                                                                </DataTemplate>
                                                            </Setter.Value>
                                                        </Setter>
                                                    </Trigger>
                                                </Style.Triggers>
                                            </Style>
                                        </Style.Resources>
                                        <Setter Property="Icon" Value="{Binding Icon, Converter={StaticResource ImageToSourceConverter}}"/>
                                        <Style.Triggers>
                                            <Trigger Property="Role" Value="SubMenuItem">
                                                <Setter Property="HeaderTemplate">
                                                    <Setter.Value>
                                                        <DataTemplate>
                                                            <StackPanel Orientation="Horizontal">
                                                                <ContentPresenter Content="{Binding Name}"/>
                                                            </StackPanel>
                                                        </DataTemplate>
                                                    </Setter.Value>
                                                </Setter>
                                            </Trigger>
                                        </Style.Triggers>
                                    </Style>
                                </MenuItem.Resources>
                            </MenuItem>
                            <MenuItem Header="Upscale well logs"/>
                            <MenuItem Header="Upscale well_top attributes"/>
                            <MenuItem Header="Upscale point attributes" Icon="{StaticResource ImagePointSet}">
                            </MenuItem>
                            <MenuItem Header="Calculate"/>
                        </ContextMenu>
                    </Button.ContextMenu>
                </Button>
        </StackPanel>
        </Border>
    </DataTemplate>

Code behind for context menu, I know it's not much but this is as far as I can get

 private void MenuItem_Click(object sender, RoutedEventArgs e)
    {
        var menuItem = sender as MenuItem;
    }

Current output

在此处输入图片说明

Please verify that you've correctly linked your code in the XAML. Something like:

<ListView SelectionChanged="Selector_OnSelectionChanged">

I note that we don't see any of your wpf xaml code. If this doesn't answer your question then it might help to add a glimpse of your xaml so we can identify why your Selector_OnSelectionChanged isn't being processed.

Thanks!

After much googling and asking around I found an answer and thought I post it for anyone who might have this problem also, just it's in the form of my problem, which should be easy enough to edit :)

<DataTemplate x:Key="AddNodeTemplate">
        <Border BorderThickness="1" Background="#F7F7F7">
            <Border.BorderBrush>
                <DrawingBrush Viewport="8,8,8,8" ViewportUnits="Absolute" TileMode="Tile">
                    <DrawingBrush.Drawing>
                        <DrawingGroup>
                            <GeometryDrawing Brush="#F7F7F7">
                                <GeometryDrawing.Geometry>
                                    <GeometryGroup>
                                        <RectangleGeometry Rect="0,0,50,50"/>
                                        <RectangleGeometry Rect="50,50,50,50"/>
                                    </GeometryGroup>
                                </GeometryDrawing.Geometry>
                            </GeometryDrawing>
                        </DrawingGroup>
                    </DrawingBrush.Drawing>
                </DrawingBrush>
            </Border.BorderBrush>
            <StackPanel>
                <Button x:Name="ButtonAdd" Click="ButtonAdd_Click" Height="30" Width="130" Tag="{Binding RelativeSource={RelativeSource AncestorType={x:Type syncfusion:Node}}}">
                    <Button.Style>
                        <Style TargetType="Button">
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate TargetType="Button">
                                        <Grid Background="#F7F7F7">
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="25"/>
                                                <ColumnDefinition/>
                                            </Grid.ColumnDefinitions>
                                            <Image Source="Images/icon_plus.bmp" HorizontalAlignment="Left" Margin="5,0,0,0"/>
                                            <TextBlock Text="Add Property" HorizontalAlignment="Center" Grid.Column="1" VerticalAlignment="Center" Foreground="LightGray" FontStyle="Italic" FontSize="12"/>
                                        </Grid>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                            <Setter Property="Background" Value="#F7F7F7"/>
                        </Style>
                    </Button.Style>
                    <Button.ContextMenu>
                        <ContextMenu>
                            <MenuItem Header="Copy Exisiting" ItemsSource="{Binding Path=AvailableProperties}" Click="AddExistingProperty_OnClick" Icon="Images/Copy.bmp" FontFamily="MS Reference Sans Serif">
                                <MenuItem.Resources>
                                    <Style TargetType="MenuItem">
                                        <Style.Resources>
                                            <Image x:Key="img" x:Shared="False" Width="10" Height="10" Source="{Binding Icon, Converter={StaticResource ImageToSourceConverter}}" 
                                                   Margin="3" VerticalAlignment="Center"/>
                                            <Style TargetType="ContentPresenter">
                                                <Style.Triggers>
                                                    <Trigger Property="ContentSource" Value="Icon">
                                                        <Setter Property="ContentTemplate">
                                                            <Setter.Value>
                                                                <DataTemplate>
                                                                    <Image Source="{Binding}"/>
                                                                </DataTemplate>
                                                            </Setter.Value>
                                                        </Setter>
                                                    </Trigger>
                                                </Style.Triggers>
                                            </Style>
                                        </Style.Resources>
                                        <Setter Property="Icon" Value="{Binding Icon, Converter={StaticResource ImageToSourceConverter}}"/>
                                        <Style.Triggers>
                                            <Trigger Property="Role" Value="SubMenuItem">
                                                <Setter Property="HeaderTemplate">
                                                    <Setter.Value>
                                                        <DataTemplate>
                                                            <StackPanel Orientation="Horizontal">
                                                                <ContentPresenter Content="{Binding Name}"/>
                                                            </StackPanel>
                                                        </DataTemplate>
                                                    </Setter.Value>
                                                </Setter>
                                            </Trigger>
                                        </Style.Triggers>
                                    </Style>
                                </MenuItem.Resources>
                            </MenuItem>
                            <MenuItem Header="Upscale well logs" Click="AddProperty_OnClick" Tag="UpscaleWellLogs" Style="{StaticResource MenuItemIcon}" Icon="Images/WellLogs.png" FontFamily="MS Reference Sans Serif"/>
                            <MenuItem Header="Upscale well top attributes" Click="AddProperty_OnClick" Tag="UpscaleWellTopAttributes" Style="{StaticResource MenuItemIcon}" Icon="Images/WellTop.png" FontFamily="MS Reference Sans Serif"/>
                            <MenuItem Header="Upscale point attributes" Click="AddProperty_OnClick" Tag="UpscalePointAttributes" Style="{StaticResource MenuItemIcon}" Icon="Images/PointSet.png" FontFamily="MS Reference Sans Serif"/>
                            <MenuItem Header="Calculate" Click="AddProperty_OnClick" Tag="Calculate" Style="{StaticResource MenuItemIcon}" Icon="Images/calculator.png" FontFamily="MS Reference Sans Serif"/>
                        </ContextMenu>
                    </Button.ContextMenu>
                </Button>
        </StackPanel>
        </Border>
    </DataTemplate>

private void AddExistingProperty_OnClick(object sender, RoutedEventArgs e)
    {
        var menuItem = e.OriginalSource as MenuItem;
        var item = menuItem.DataContext as PropertyNode;
        viewModel.AddPropertyNode(item);
    }

    private void AddProperty_OnClick(object sender, RoutedEventArgs e)
    {
        if (MenuItemActivated != null)
        {
            var menuItem = sender as MenuItem;
            var command = menuItem.Tag as CognitiveTreeMenuCommand?;
            if (command.HasValue)
            {
                MenuItemActivated(this, new MenuItemEventHandlerArgs() { Command = command.Value });
            }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM