microsoft / microsoft-ui-xaml

WinUI: a modern UI framework with a rich set of controls and styles to build dynamic and high-performing Windows applications.
MIT License
6.38k stars 683 forks source link

UnauthorizedAccessException When Setting Content of ReadOnly RichEditBox #10028

Open D4rk-S0ul opened 2 months ago

D4rk-S0ul commented 2 months ago

Describe the bug

Settings the Content of a RichEditBox in CodeBehind works, but only if IsReadOnly="False". As soon as I make it ReadOnly, I get the follwing exeption:

System.UnauthorizedAccessException
  HResult=0x80070005
  Message=Attempted to perform an unauthorized operation.
  Source=WinRT.Runtime
  StackTrace:
   at WinRT.ExceptionHelpers.<ThrowExceptionForHR>g__Throw|38_0(Int32 hr)
   at WinRT.ExceptionHelpers.ThrowExceptionForHR(Int32 hr)
   at ABI.Microsoft.UI.Text.ITextDocumentMethods.SetText(IObjectReference _obj, TextSetOptions options, String value)
   at Microsoft.UI.Text.RichEditTextDocument.SetText(TextSetOptions options, String value)
   at RichEditBoxBug.MainWindow.RichEditBox_Loaded(Object sender, RoutedEventArgs e) in C:\Users\User\Documents\Programming\RichEditBoxBug\RichEditBoxBug\MainWindow.xaml.cs:line 26
   at WinRT._EventSource_global__Microsoft_UI_Xaml_RoutedEventHandler.EventState.<GetEventInvoke>b__1_0(Object sender, RoutedEventArgs e)
   at ABI.Microsoft.UI.Xaml.RoutedEventHandler.Do_Abi_Invoke(IntPtr thisPtr, IntPtr sender, IntPtr e)

Steps to reproduce the bug

  1. Create a new packaged app
  2. Add a RichEditBox
  3. Set it's content in CodeBehind using richEditBox.Document.SetText(TextSetOptions.None, "Hello, World!");

Alternatively, the source-code can be found on my GitHub.

Expected behavior

Even according to the official documentation: IsReadOnly gets or sets a value that indicates whether the user can change the text in the RichEditBox.

Therefore, I expect to be able to set the content of a ReadOnly RichEditBox programmatically. The only effect the ReadOnly state should have is preventing UserInput.

Screenshots

No response

NuGet package version

WinUI 3 - Windows App SDK 1.6.0: 1.6.240829007

Windows version

No response

Additional context

Windows Version: Windows 11 (23H2): Build 22631.4169 --> Please add this Version to the selectable Windows Versions

D4rk-S0ul commented 3 weeks ago

Is there any further information needed? Just let me know