{"record":{"id":"c690c751069a2d5b","repo":"dotnet/wpf","slug":"sr-inputlanguagemanager-notreadytochangecurrentlanguage","errorCode":null,"errorMessage":"SR.InputLanguageManager_NotReadyToChangeCurrentLanguage","messagePattern":"SR\\.InputLanguageManager_NotReadyToChangeCurrentLanguage","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/InputLanguageManager.cs","lineNumber":459,"sourceCode":"                int count = SafeNativeMethods.GetKeyboardLayoutList(0, null);\n\n                return (count > 1);\n            }\n        }\n\n        #endregion Internal Properties\n \n        //------------------------------------------------------\n        //\n        //  Private Methods\n        //\n        //------------------------------------------------------\n\n        private void SetSourceCurrentLanguageId(CultureInfo languageId)\n        {\n            if (_source == null)\n            {\n                throw new InvalidOperationException(SR.InputLanguageManager_NotReadyToChangeCurrentLanguage);\n            }\n\n            _source.CurrentInputLanguage = languageId;\n        }\n\n        //------------------------------------------------------\n        //\n        //  Private Events\n        //\n        //------------------------------------------------------\n\n        /// <summary>\n        ///     This is an event when the input language is changed.\n        /// </summary>\n        private event InputLanguageEventHandler _InputLanguageChanged;\n\n        /// <summary>\n        ///     This is an event when the input language is being changed.","sourceCodeStart":441,"sourceCodeEnd":477,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/InputLanguageManager.cs#L441-L477","documentation":"InputLanguageManager.SetSourceCurrentLanguageId throws InvalidOperationException when the manager has no associated PresentationSource yet (_source == null). CurrentInputLanguage can only be forwarded to a source once the manager is attached to one. Called from CurrentInputLanguage and Focus, it means the caller is setting the input language before the element is connected to a presentation source.","triggerScenarios":"Setting InputLanguageManager.CurrentInputLanguage or calling Focus on an element whose InputLanguageManager has not yet been associated with a PresentationSource (e.g. element not yet in a visual tree, or called during construction before the window is shown).","commonSituations":"Manipulating input language in a window constructor or before the first layout/render pass; headless or off-screen element scenarios where the HwndSource is not yet created.","solutions":["Defer the call until after the element/window is loaded and shown (e.g. in the Loaded event handler) so the PresentationSource exists.","Check PresentationSource.FromVisual(element) != null before touching CurrentInputLanguage.","If input language must be tracked early, listen for SourceInitialized on the Window and set the language there."],"exampleFix":"// before\nInputLanguageManager.CurrentInputLanguage = new CultureInfo(\"ja-JP\"); // in constructor\n// after\nwindow.SourceInitialized += (s, e) =>\n    InputLanguageManager.CurrentInputLanguage = new CultureInfo(\"ja-JP\");","handlingStrategy":"validation","validationCode":"if (PresentationSource.FromVisual(myElement) == null)\n    throw new InvalidOperationException(\"Element not attached to a PresentationSource yet\");\nInputLanguageManager.CurrentInputLanguage = culture;","typeGuard":"static bool CanSetInputLanguage(DispatcherObject obj) =>\n    PresentationSource.FromDependencyObject(obj as DependencyObject) != null;","tryCatchPattern":"try { mgr.CurrentInputLanguage = culture; }\ncatch (InvalidOperationException) { /* retry on Loaded */ }","preventionTips":["Set input language in Window.SourceInitialized or element Loaded, never in constructors.","Check PresentationSource.FromVisual(element) != null first.","For headless/off-screen scenarios, avoid the InputLanguageManager entirely."],"tags":["wpf","input","localization","invalid-state"],"backgroundTag":"invalid-state-transition","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}