用于wpf中用户控制的上下文菜单

时间:2013-12-26 09:24:39

标签: c# wpf mvvm contextmenu

我正在使用MVVM模式开发WPF应用程序。我使用用户控件。 我想做一些观看/复制&通过用户控件中的上下文菜单粘贴。

我能够在列表视图/网格视图等中添加用户上下文菜单。我能够使用样式。

我希望直接向用户控件添加一些上下文菜单。我尝试了以下代码,它不起作用:(

任何人都可以帮忙吗?

UserControl x:Class="MYOWN.ScenarioView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:SMTF" 
    mc:Ignorable="d" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" d:DesignHeight="392" d:DesignWidth="484"  >

<UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="../../DefaultStyle.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</UserControl.Resources>

<UserControl.ContextMenu>
    <ContextMenu Visibility="Visible">
        <MenuItem Header="Standard MenuItem 2" />
        <MenuItem Header="Standard MenuItem 3" />
    </ContextMenu>
</UserControl.ContextMenu>
<Grid Height="394" Width="486" >

    <Label Content="Category" Height="28" HorizontalAlignment="Left" Margin="13,36,0,0" Name="lblCategory" VerticalAlignment="Top" />
    <TextBox Height="23" HorizontalAlignment="Left" Margin="118,106,0,0" Name="txtScenario" VerticalAlignment="Top" Width="343" Text="{Binding Scenario_Desc}" />
    <Button Content="Save" Command="{Binding SaveData}" Height="23" HorizontalAlignment="Left" Margin="294,348,0,0" Name="btnSave" VerticalAlignment="Top" Width="75" />
    <Button Content="Reset" Command="{Binding ClearData}" Height="23" HorizontalAlignment="Left" Margin="386,348,0,0" Name="btnReset" VerticalAlignment="Top" Width="75" />
    <Label Content="Sub Category" Height="28" HorizontalAlignment="Left" Margin="13,70,0,0" Name="lblSubCategory" VerticalAlignment="Top" Grid.RowSpan="2" Grid.ColumnSpan="2" />
    <ComboBox Height="23" HorizontalAlignment="Left" Margin="118,36,0,0" Name="cboCategory" VerticalAlignment="Top" Width="343" 
             ItemsSource="{Binding Path=Category}"
             DisplayMemberPath="Category_Desc"
             SelectedValuePath="Category_Id"
             SelectedValue="{Binding Path=Category_Id}"
             SelectedIndex="0"
             Text="{Binding Category_Desc}">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="SelectionChanged">
                <i:InvokeCommandAction Command="{Binding CategorySelected}"
                                   CommandParameter="{Binding SelectedValue, ElementName=cboCategory}"/>
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </ComboBox>



    <Label Content="Scenario" Height="28" HorizontalAlignment="Left" Margin="15,104,0,0" Name="lblScenario" VerticalAlignment="Top" />
    <ComboBox  Height="23" HorizontalAlignment="Left" VerticalAlignment="Top" Width="343" ItemsSource="{Binding Path=SubCategory}" Margin="118,70,0,0" Name="cboSubCategory" 
               DisplayMemberPath="Sub_Category_Desc"
               SelectedValue="{Binding Path=Sub_Category_Id}" 
               SelectedValuePath="Sub_Category_Id" 
               Text="{Binding Sub_Category_Desc}"  />

    <GroupBox Header=" Script " Height="200" HorizontalAlignment="Left" Margin="13,135,0,0" Name="grpScript" VerticalAlignment="Top" Width="458">
        <Grid>
            <TextBlock HorizontalAlignment="Left" Margin="30,00,0,100" Height="26">
            <ToggleButton  x:Name="Add" Focusable="False" Command ="{Binding ShowNew}" Style="{StaticResource SMToggle}" >
                <Image Source="/Image/Add.png"  Width="16" Height="16" />
            </ToggleButton>
           <ToggleButton  x:Name="Select" Focusable="False" Command ="{Binding ShowSelect}" Style="{StaticResource SMToggle}" >
                <Image Source="/Image/select.png"  Width="16" Height="16" />
            </ToggleButton>

            </TextBlock>

            <ListView  AlternationCount="2"  Height="160" Width="343" HorizontalAlignment="Left" Margin="97,10,0,0" Name="lvTestCase" VerticalAlignment="Top" 
                       ItemContainerStyle="{StaticResource ListViewItemStyle}" ItemsSource="{Binding Script}" >
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="MouseDoubleClick">
                        <i:InvokeCommandAction Command="{Binding EditData}"/>
                    </i:EventTrigger>
                </i:Interaction.Triggers>
                <ListView.View>
                    <GridView x:Name="grdScript">
                        <GridViewColumn DisplayMemberBinding="{Binding Path=Script_Text}" Header="Script" Width="315" />
                    </GridView>
                </ListView.View>

            </ListView>
        </Grid>
    </GroupBox>
</Grid>

0 个答案:

没有答案