[英]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.