Context menu ApplicationCommands.Cut still enabled when IsReadOnly = true #444

Open
opened 2025-11-01 21:48:52 -05:00 by CodingOctocat · 0 comments
CodingOctocat commented 2025-11-01 21:48:52 -05:00 (Migrated from github.com)

Description

When a TextEditor is set to IsReadOnly = true, context menu items (or any UI elements) bound to standard ApplicationCommands.Cut remain enabled, even though those operations have no effect.
This behavior is inconsistent with the expected WPF command pattern, where CanExecute should return false when the editor is read-only.

If add CommandTarget, it always not work.
CommandTarget="{Binding PlacementTarget, RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}}"

Image

Example Code

<avalonedit:TextEditor x:Name="editor" IsReadOnly="True">
    <avalonedit:TextEditor.ContextMenu>
        <ContextMenu>
            <MenuItem Header="Cut" Command="ApplicationCommands.Cut" /> ❌ always enabled

            <MenuItem Command="{x:Static ApplicationCommands.Cut}"
                      IsEnabled="{Binding PlacementTarget.IsReadOnly,
                                 RelativeSource={RelativeSource AncestorType=ContextMenu},
                                 Converter={StaticResource Boolean2BooleanReConverter}}" /> ✅

            <MenuItem Header="Copy" Command="ApplicationCommands.Copy" /> ✅

            <MenuItem Command="{x:Static ApplicationCommands.Copy}"
                      CommandTarget="{Binding PlacementTarget,
                        RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}}" /> ❌ always disabled

            <MenuItem Header="Paste" Command="ApplicationCommands.Paste" /> ✅
        </ContextMenu>
    </avalonedit:TextEditor.ContextMenu>
</avalonedit:TextEditor>
## Description When a TextEditor is set to `IsReadOnly = true`, context menu items (or any UI elements) bound to standard `ApplicationCommands.Cut` remain enabled, even though those operations have no effect. This behavior is inconsistent with the expected WPF command pattern, where CanExecute should return false when the editor is read-only. If add CommandTarget, it always not work. `CommandTarget="{Binding PlacementTarget, RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}}"` <img width="485" height="345" alt="Image" src="https://github.com/user-attachments/assets/f0ad8bb6-a056-4e3f-9731-8cd20e9ce385" /> ## Example Code ```xaml <avalonedit:TextEditor x:Name="editor" IsReadOnly="True"> <avalonedit:TextEditor.ContextMenu> <ContextMenu> <MenuItem Header="Cut" Command="ApplicationCommands.Cut" /> ❌ always enabled <MenuItem Command="{x:Static ApplicationCommands.Cut}" IsEnabled="{Binding PlacementTarget.IsReadOnly, RelativeSource={RelativeSource AncestorType=ContextMenu}, Converter={StaticResource Boolean2BooleanReConverter}}" /> ✅ <MenuItem Header="Copy" Command="ApplicationCommands.Copy" /> ✅ <MenuItem Command="{x:Static ApplicationCommands.Copy}" CommandTarget="{Binding PlacementTarget, RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}}" /> ❌ always disabled <MenuItem Header="Paste" Command="ApplicationCommands.Paste" /> ✅ </ContextMenu> </avalonedit:TextEditor.ContextMenu> </avalonedit:TextEditor> ```
Sign in to join this conversation.
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
gh-icsharpcode/AvalonEdit#444
No description provided.