microsoft / microsoft/microsoft-ui-xaml
UnauthorizedAccessException When Setting Content of ReadOnly RichEditBox
Nobody has claimed this yet.
- Dominant language
- C++
- Stars
- 8.4k
- Forks
- 942
- Avg merge
- 2d 7h
- Merged PRs (30d)
- 105
Description
### 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:
```ex
System.UnauthorizedAccessException
HResult=0x80070005
Message=Attempted to perform an unauthorized operation.
Source=WinRT.Runtime
StackTrace:
at WinRT.ExceptionHelpers.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.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](https://github.com/D4rk-S0ul/RichEditBoxBug) can be found on my GitHub.
### Expected behavior
Even according to the [official documentation](https://learn.microsoft.com/en-us/uwp/api/windows.ui.xaml.controls.richeditbox?view=winrt-26100):
`IsReadOnly` gets or sets a value that indicates whether the **user** can change the text in the [RichEditBox](https://learn.microsoft.com/en-us/uwp/api/windows.ui.xaml.controls.richeditbox?view=winrt-26100).
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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the linked RichEditBox reproduction and MainWindow.xaml.cs line 26, then run it with IsReadOnly set to true and false. Compare the behavior of Document.SetText(TextSetOptions.None, "Hello, World!") with the documented RichEditBox behavior; done means programmatic content assignment works while user editing remains disabled.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- desktop
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100