在ListBox中显示元素的ContextMenu

时间:2017-08-10 09:49:12

标签: c# wpf listbox

问题:

我有一个ListBox,我使用DataBinding显示我的数据。数据显示为自定义视图SystemEnvironmentElementView

我想允许用户重命名单个元素。这应该发生在所需元素的ViewModel中。

我在显示我的数据的ContextMenu中创建了View。但是,当我右键单击我的元素时,不会显示ContextMenu。我还遇到了一个问题,即在我的PreviewMouseDown事件处理程序中,我不会在OriginalSource中将EventArgs视为Border,而是<customControls:ListBoxNoDragSelection ItemsSource="{Binding Elements}" Background="{DynamicResource BG}" SelectedItem="{Binding SystemEnviromentAnalysis.SelectedElement}" BorderThickness="0" x:Name="ListBoxNoDragSelection" SelectedValue="{Binding SelectedElement}"> <i:Interaction.Behaviors> <dragAndDrop:FrameworkElementDropBehaviorWithMousePos /> </i:Interaction.Behaviors> <ListBox.ItemsPanel> <ItemsPanelTemplate> <Canvas Background="{DynamicResource MyVisualBrush}" Height="Auto" Width="Auto" PreviewMouseLeftButtonDown="UIElement_OnPreviewMouseLeftButtonDown"/> </ItemsPanelTemplate> </ListBox.ItemsPanel> <ListBox.ItemContainerStyle> <Style TargetType="ListBoxItem"> <Setter Property="Canvas.Left" Value="{Binding Element.X}"/> <Setter Property="Canvas.Top" Value="{Binding Element.Y}"/> <Setter Property="Canvas.ZIndex" Value="{Binding Element.Z}"/> <Setter Property="Height" Value="Auto"/> <Setter Property="Width" Value="Auto"/> </Style> </ListBox.ItemContainerStyle> <ListBox.ItemTemplate> <DataTemplate> <systemEnvironment:SystemEnvironmentElementView/> </DataTemplate> </ListBox.ItemTemplate> </customControls:ListBoxNoDragSelection>

问题:

如何让我的元素的ContextMenu显示?

列表框代码:

<UserControl x:Class="TestApp.Editor.Views.SystemEnvironment.SystemEnvironmentElementView"
    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"
         mc:Ignorable="d"
    Height="Auto" Width="Auto">
<UserControl.ContextMenu>
    <ContextMenu>
        <MenuItem Header="Rename"/>
        </ContextMenu>
</UserControl.ContextMenu>    
    <StackPanel>
    <Image Height="{Binding Element.Height}" Width="{Binding Element.Width}" Grid.Row="0" Source="{Binding Element.Icon}" IsHitTestVisible="False"/>
        <Label Height="Auto" Width="Auto" Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center" IsHitTestVisible="False" 
           Content="{Binding Element.Name}"/>
    </StackPanel>

SystemEnviromentElementView:

      MenuItem tourchmeMenuItem; // Declare Global .......

 public boolean onCreateOptionsMenu(Menu menu) 
 {
        getMenuInflater().inflate(R.menu.search, menu);
        menu.findItem(R.id.action_search).setVisible(false);
        tourchmeMenuItem = menu.findItem(R.id.done);
        return true;
 }

    public boolean onOptionsItemSelected(MenuItem item) {

    case R.id.done:
                       if(LoginPreferences.getActiveInstance(CustomViewFinderScannerActivity.this).getIsFlashLight()){
                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                                mScannerView.setFlash(false);
                                LoginPreferences.getActiveInstance(CustomViewFinderScannerActivity.this).setIsFlashLight(false);
                                tourchmeMenuItem.setIcon(getResources().getDrawable(R.mipmap.torch_white_32));
                            }
                        }else {
                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                                mScannerView.setFlash(true);
                                LoginPreferences.getActiveInstance(CustomViewFinderScannerActivity.this).setIsFlashLight(true);
                                tourchmeMenuItem.setIcon(getResources().getDrawable(R.mipmap.torch_cross_white_32));
                            }
                        }
                        break;

}

0 个答案:

没有答案