简体   繁体   English

如何在单击时创建的“上下文”菜单项中添加组件?

[英]How to add a component to Context menu item which will create on click?

I have context manu: 我有上下文菜单:

<ContextMenu x:Key="MyContextMenu">
<MenuItem Header="ChangeColor">
</MenuItem>

I need to add the Color Picker at the menu item (ChangeColor) and after click on it want the color picker shows up. 我需要在菜单项(ChangeColor)中添加拾色器,然后单击它以显示拾色器。 How can i do it? 我该怎么做? Thanks. 谢谢。

If you are using MVVM add a Command to the ViewModel and bind your MenuItem to that command which will be triggered on click. 如果您使用的是MVVM,则将一个Command添加到ViewModel并将您的MenuItem绑定到该命令,该命令将在单击时触发。 Then Popup a dialog with your colorpicker in it. 然后弹出一个对话框,其中包含您的颜色选择器。 Catch your DialogResult and handle appropriate on this. 捕获您的DialogResult并对此进行适当处理。

Here's an example of how you could do this 这是您如何执行此操作的示例

MainViewModel MainViewModel

public class MainViewModel
{
    private ColorPickerWindow _colorPicker;


    public MainViewModel()
    {
        ShowColorPicker = new DelegateCommand(ShowColorPickerExecute, () => true);
        Ok = new DelegateCommand(OkExecuted, () => true);
        Ok = new DelegateCommand(CancelExecuted, () => true);

        // Create windows instance and set the DataContext to MainViewModel
        _colorPicker = new ColorPickerWindow();
        _colorPicker.DataContext = this;
    }


    public Color Color { get; set; }

    public ICommand ShowColorPicker { get; set; }

    public ICommand Ok { get; set; }
    public ICommand Cancel { get; set; }

    private void CancelExecuted()
    {
        _colorPicker.DialogResult = false;
        _colorPicker.Close();
    }

    private void OkExecuted()
    {
        _colorPicker.DialogResult = true;
        _colorPicker.Close();
    }

    private void ShowColorPickerExecute()
    {
        //Show the ColorPickerWindow
        if (_colorPicker.ShowDialog().GetValueOrDefault(false))
        {
            // Show which color is picked or do whatever you want
            MessageBox.Show(Color.ToString());
        }
        else
        {
            MessageBox.Show(Color.ToString());
        }
    }
}

MainWindow (XAML) 主窗口(XAML)

<Window x:Class="WpfApplication2.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525"
    >
<Window.ContextMenu>
    <ContextMenu>
        <MenuItem Command="{Binding ShowColorPicker}" Header="Choose color"/>
    </ContextMenu>
</Window.ContextMenu>
<Grid>


</Grid>

And last but not least the ColorPickerWindow (XAML) 最后但并非最不重要的一点是ColorPickerWindow(XAML)

<Window x:Class="WpfApplication2.ColorPickerWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors"
    Title="Choose color" Height="300" Width="300">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="40"/>
    </Grid.RowDefinitions>
    <dxe:ColorChooser Color="{Binding Color}"></dxe:ColorChooser>
    <StackPanel Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Right" Margin="10">
        <Button Content="OK" Width="75" IsDefault="True" Command="{Binding Ok}"/>
        <Button Content="Cancel" Width="75" IsCancel="True" Command="{Binding Cancel}"/>
    </StackPanel>
</Grid>

I Wrote this mostly out of my head so i'm not sure it will work did not test it. 我把它大部分都写在了脑海中,所以我不确定它是否可以正常工作而不进行测试。

Do not forget to set the DataContext of the MainWindow to MainViewModel 不要忘记将MainWindow的DataContext设置为MainViewModel

What you can do is: 您可以做的是:

<ContextMenu>
    <MenuItem Header="ChangeColor" Click="ChangeColor_Click"/>    
</ContextMenu>

And in your CodeBehind generated method, it generates automatically with Click="ChangeColor_Click".: 在您的CodeBehind生成方法中,它会通过Click =“ ChangeColor_Click”自动生成。:

private void ChangeColor_Click(object sender, RoutedEventArgs e)
    {
        // Your logic for ChangeColor
    }

Hope helps. 希望会有所帮助。 Greetings! 问候!

UPDATE: 更新:

Sorry for responding so late. 很抱歉这么晚回复。

You must add the Coding4Fun toolkit to your solution. 您必须将Coding4Fun工具箱添加到您的解决方案中。

after in your MainPage.xaml add this statment: 在您的MainPage.xaml中添加以下语句:

xmlns:c4fToolkit="clr-namespace:Coding4Fun.Toolkit.Controls;assembly=Coding4Fun.Toolkit.Controls"

And add this lines to your " LayoutRoot " 并将此行添加到您的“ LayoutRoot ”中

<c4fToolkit:ColorPicker x:Name="colorpicker ColorChanged="picker_ColorChanged"/>

the ColorChanged method what you want is: 您想要的ColorChanged方法是:

private void picker_ColorChanged(object sender, Color color)
    {
        // As Color
        Color nwColor = color;
        // As SolidColorBrush
        SolidColorBrush nwColor1 = new SolidColorBrush(color);
    }

You can make this Color global an use it in all your solution. 您可以使此Color全局性在您的所有解决方案中使用它。

PD: For the good practice and best solution give you already Jordy van Eijk You always think to implement MVVM. PD:为获得最佳实践和最佳解决方案,已经为您提供了Jordy van Eijk您一直想实现MVVM。

Greetings! 问候!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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