Wpf 如何在用户控件上实现启用/禁用应用程序命令等行为

Wpf 如何在用户控件上实现启用/禁用应用程序命令等行为,wpf,focus,controls,toolbar,behavior,Wpf,Focus,Controls,Toolbar,Behavior,我创建了一个用户控件,它是一个工具栏,上面有几个按钮。工具栏包含一些常规功能,如复制、粘贴、撤消、重做等。这些按钮使用内置命令,如 <UserControl x:Class="Foundation.FundRaising.DataRequest.Windows.Controls.TextEditorToolBar" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmln

我创建了一个用户控件,它是一个工具栏,上面有几个按钮。工具栏包含一些常规功能,如复制、粘贴、撤消、重做等。这些按钮使用内置命令,如

<UserControl x:Class="Foundation.FundRaising.DataRequest.Windows.Controls.TextEditorToolBar"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation" 
         mc:Ignorable="d" 
         d:DesignHeight="34" d:DesignWidth="550">
<Grid>
    <telerik:RadToolBar>
        <telerik:RadToolBar.Resources>
            <Style TargetType="StackPanel">
                <Setter Property="Orientation" Value="Horizontal"/>
                <Setter Property="VerticalAlignment" Value="Center"/>
            </Style>
            <Style TargetType="Image">
                <Setter Property="Width" Value="20"/>
                <Setter Property="Height" Value="20"/>
            </Style>
        </telerik:RadToolBar.Resources>
        <telerik:RadButton Command="Cut">
            <StackPanel>
                <Image Source="/Foundation.FundRaising.DataRequest.Windows;component/Resources/cut20.png" ToolTip="Cut"/>
                <Label Content="Cut"/>
            </StackPanel>
        </telerik:RadButton>
        <telerik:RadButton Command="Copy">
            <StackPanel>
                <Image Source="/Foundation.FundRaising.DataRequest.Windows;component/Resources/copy20.png" ToolTip="Copy"/>
                <Label Content="Copy"/>
            </StackPanel>
        </telerik:RadButton>
        <telerik:RadButton Command="Paste">
            <StackPanel>
                <Image Source="/Foundation.FundRaising.DataRequest.Windows;component/Resources/paste20.png" ToolTip="Paste"/>
                <Label Content="Paste"/>
            </StackPanel>
        </telerik:RadButton>
        <telerik:RadButton Command="Delete">
            <StackPanel>
                <Image Source="/Foundation.FundRaising.DataRequest.Windows;component/Resources/delete24.png" ToolTip="Delete"/>
                <Label Content="Delete"/>
            </StackPanel>
        </telerik:RadButton>

        <telerik:RadToolBarSeparator/>

        <telerik:RadButton Command="Undo">
            <StackPanel>
                <Image Source="/Foundation.FundRaising.DataRequest.Windows;component/Resources/undo20.png" ToolTip="Undo"/>
                <Label Content="Undo"/>
            </StackPanel>
        </telerik:RadButton>
        <telerik:RadButton Command="Redo">
            <StackPanel>
                <Image Source="/Foundation.FundRaising.DataRequest.Windows;component/Resources/redo20.png" ToolTip="Redo"/>
                <Label Content="Redo"/>
            </StackPanel>
        </telerik:RadButton>
        <telerik:RadButton x:Name="radButtonSpellCheck" Click="RadButtonSpellCheckClick">
            <StackPanel>
                <Image Source="/Foundation.FundRaising.DataRequest.Windows;component/Resources/spell_check24.png" ToolTip="Spell Check"/>
                <Label Content="Spell Check"/>
            </StackPanel>
        </telerik:RadButton>            
    </telerik:RadToolBar>
</Grid>

所有这些都可以正常工作,但我想做的是,当控件像内置命令一样聚焦时,启用/禁用“拼写检查”按钮,而不必捕获异常并向用户显示警报。基本上,确定聚焦控件是否能够接受“功能”

将按钮的可见性绑定到控件的IsKeyboardFocused属性,并创建转换器将布尔转换为可见性感谢快速响应。当您说“控件的属性和…”时,您是指具有焦点的控件吗?如果是这样的话,我们将如何获得通往该控件的路径?IsEnabled=“{Binding ElementName=??,Path=IsKeyboardFocused}”
我将路径设置为等于我的用户控件的x:Name,但按钮没有变为“enabled”。另外,我没有看到任何绑定错误。请尝试使用IsKeyboardFocusWithin,不要忘记将模式设置为单向
    private void RadButtonSpellCheckClick(object sender, RoutedEventArgs e)
    {
        try
        {
            IInputElement control = Keyboard.FocusedElement;
            RadSpellChecker.Check((Control)control, SpellCheckingMode.AllAtOnce);
        }
        catch (NotImplementedException)
        {
            RadWindow.Alert("You have selected a control that does not support the spell check feature.");
        }
    }