I have been trying to use the code in the reply to Question "Editable combobox overwrites my context menu" asked on asked Aug 30 '12 at 14:24 But it always errors with a nullreferenceerror on
(VisualTreeHelper.GetChild(VisualTreeHelper.GetChild(combobox, 0), 2) as TextBox).ContextMenu = combobox.ContextMenu;
In order to minimize any environment problems I have added the code as a new project
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://ift.tt/o66D3f"
xmlns:x="http://ift.tt/mPTqtT"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ComboBox IsEditable="True" ContextMenuService.ShowOnDisabled="True"
Name="combobox" Loaded="combobox_Loaded">
<ComboBox.ContextMenu>
<ContextMenu>
<MenuItem Header="test"></MenuItem>
</ContextMenu>
</ComboBox.ContextMenu>
<ComboBoxItem Content="Item1"></ComboBoxItem>
<ComboBoxItem Content="Item2"></ComboBoxItem>
</ComboBox>
</Grid>
</Window>
code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void combobox_Loaded(object sender, RoutedEventArgs e)
{
(VisualTreeHelper.GetChild(VisualTreeHelper.GetChild(combobox, 0), 2) as TextBox).ContextMenu = combobox.ContextMenu;
}
}
}
Aucun commentaire:
Enregistrer un commentaire