[英]How to add a component to Context menu item which will create on click?
我有上下文菜单:
<ContextMenu x:Key="MyContextMenu">
<MenuItem Header="ChangeColor">
</MenuItem>
我需要在菜单项(ChangeColor)中添加拾色器,然后单击它以显示拾色器。 我该怎么做? 谢谢。
如果您使用的是MVVM,则将一个Command添加到ViewModel并将您的MenuItem绑定到该命令,该命令将在单击时触发。 然后弹出一个对话框,其中包含您的颜色选择器。 捕获您的DialogResult并对此进行适当处理。
这是您如何执行此操作的示例
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());
}
}
}
主窗口(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>
最后但并非最不重要的一点是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>
我把它大部分都写在了脑海中,所以我不确定它是否可以正常工作而不进行测试。
不要忘记将MainWindow的DataContext设置为MainViewModel
您可以做的是:
<ContextMenu>
<MenuItem Header="ChangeColor" Click="ChangeColor_Click"/>
</ContextMenu>
在您的CodeBehind生成方法中,它会通过Click =“ ChangeColor_Click”自动生成。:
private void ChangeColor_Click(object sender, RoutedEventArgs e)
{
// Your logic for ChangeColor
}
希望会有所帮助。 问候!
更新:
很抱歉这么晚回复。
您必须将Coding4Fun工具箱添加到您的解决方案中。
在您的MainPage.xaml中添加以下语句:
xmlns:c4fToolkit="clr-namespace:Coding4Fun.Toolkit.Controls;assembly=Coding4Fun.Toolkit.Controls"
并将此行添加到您的“ LayoutRoot ”中
<c4fToolkit:ColorPicker x:Name="colorpicker ColorChanged="picker_ColorChanged"/>
您想要的ColorChanged方法是:
private void picker_ColorChanged(object sender, Color color)
{
// As Color
Color nwColor = color;
// As SolidColorBrush
SolidColorBrush nwColor1 = new SolidColorBrush(color);
}
您可以使此Color全局性在您的所有解决方案中使用它。
PD:为获得最佳实践和最佳解决方案,已经为您提供了Jordy van Eijk您一直想实现MVVM。
问候!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.