// ***********************************************************************
// some other content here
// some other content here
// ***********************************************************************
public class TestClass
{
}NullReferenceException thrown on opening the CompletionListWindow when RadSyntaxEditor is hosted in a non-WPF application (usually WinForms) and there are no WPF Application and MainWindow initialized.
This can happen if the RadSyntaxEditor for WPF is hosted in a WinForms application. In this scenario the System.Windows.Application.Current and its MainWindow are not initialized, which is what the IntelliPrompt positioning logic relies on.
Exception details:
System.NullReferenceException: 'Object reference not set to an instance of an object.'
> Telerik.Windows.Controls.SyntaxEditor.dll!Telerik.Windows.Controls.SyntaxEditor.UI.IntelliPromptBase.SetPosition(System.Windows.Point pointInScreen) Line 452 C# Telerik.Windows.Controls.SyntaxEditor.dll!Telerik.Windows.Controls.SyntaxEditor.UI.IntelliPromptBase.SetPosition(System.Windows.Point pointInEditorPresenter, System.Windows.Point pointAboveTheCaret) Line 442 C# Telerik.Windows.Controls.SyntaxEditor.dll!Telerik.Windows.Controls.SyntaxEditor.UI.IntelliPromptBase.SetPositionInView() Line 480 C# Telerik.Windows.Controls.SyntaxEditor.dll!Telerik.Windows.Controls.SyntaxEditor.UI.IntelliPromptBase.InitializeIntellisense() Line 401 C# Telerik.Windows.Controls.SyntaxEditor.dll!Telerik.Windows.Controls.SyntaxEditor.UI.IntelliPromptBase.Show(Telerik.Windows.Controls.SyntaxEditor.UI.CaretPosition caretStartPosition, Telerik.Windows.Controls.SyntaxEditor.UI.CaretPosition caretEndPositions) Line 211 C#
Telerik.Windows.Controls.SyntaxEditor.dll!Telerik.Windows.Controls.SyntaxEditor.UI.IntelliPromptBase.Show() Line 182 C#To work this around, you can initialize a new WPF application and MainWindow and create a hidden HwndSource for the MainWindow. This should happen before showing the WPF content in the WinForms application's code.
public Form1()
{
InitializeComponent();
new System.Windows.Application();
System.Windows.Application.Current.MainWindow = new System.Windows.Window();
var parameters = new HwndSourceParameters("HiddenHost")
{
Width = 1, Height = 1,
WindowStyle = unchecked((int)0x80000000) // WS_POPUP | WS_EX_NOACTIVATE
};
var hwndSource = new HwndSource(parameters);
hwndSource.RootVisual = System.Windows.Application.Current.MainWindow;
// other code here
ElementHost host = new ElementHost { Dock = DockStyle.Fill };
host.Child = wpfControl
this.Controls.Add(host);
}
The position of the popup representing the IntelliPrompt is wrong when shown on the monitor with the higher DPI scale, in a setup with two monitors with different DPI settings. For example, if one monitor has 100% DPI scale and the other 125%, the position of the popup will be wrong when the app is on the 125% monitor.
To work this around, use the Caret visual as the PlacementTarget for the popup and set its offsets to 0 after it is shown.
private void RadSyntaxEditor_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
if (KeyboardModifiers.IsControlDown && e.Key == Key.Space)
{
e.Handled = true;
var editor = (RadSyntaxEditor)sender;
CaretPosition startPosition = new CaretPosition(editor.CaretPosition);
CaretPosition endPosition = new CaretPosition(editor.CaretPosition);
editor.IntelliPrompts.CompletionListWindow.Show(startPosition, endPosition);
editor.IntelliPrompts.CompletionListWindow.HorizontalOffset = 0;
editor.IntelliPrompts.CompletionListWindow.VerticalOffset = 0;
}
}
private void RadSyntaxEditor_Loaded(object sender, System.Windows.RoutedEventArgs e)
{
var editor = (RadSyntaxEditor)sender;
var caret = editor.FindChildByType<Telerik.Windows.Controls.SyntaxEditor.UI.Caret>();
editor.IntelliPrompts.CompletionListWindow.PlacementTarget = caret;
editor.IntelliPrompts.CompletionListWindow.Placement = System.Windows.Controls.Primitives.PlacementMode.Bottom;
}