访问userControl中richTextBox的PreviewKeyDown事件

发布于 2024-10-11 12:46:15 字数 2005 浏览 4 评论 0原文

我想知道如何访问UserControlRichTextBox的事件PreviewKeyDown

例如,我有用户控件,并且在此用户控件中我只有一个 richTextBox:

类似这样的内容:

<UserControl x:Class="Spirit.Controls.RichTextBoxControl"
             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:toolkit="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit.Extended" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <toolkit:RichTextBox Name="RichTextBox" 
                             Grid.Row="0" PreviewKeyDown="?">

       </toolkit:RichTextBox>
    </Grid>
</UserControl>

我在 WPF 窗口中使用此控件。

<Window x:Class="WpfApplication2.Window2"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:Controls="clr-namespace:WpfApplication2.Controls" xmlns:WpfApplication2="clr-namespace:WpfApplication2" Title="Window2" Height="300" Width="300">
    <Grid>

        <Spirit.Controls:RichTextBoxControl Background="Red"
                                            FontSize="13"
                                            Margin="4,4,4,4" 
                                            Grid.Row="0" 
                                            Here I would like to acces to PreviewKeyDown of richTextBox/>
    </Grid>
</Window>

我希望能够访问 richTextBox 的 PreviewKeyDown,在此事件上绑定一些方法并能够访问 KeyEventArgs。

像这样的事情:

   private void RichTextBoxInUserControl_PreviewKeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Enter )
        {
         //...
        }
    }

I would like to know how can I get access to event PreviewKeyDown of RichTextBox in UserControl.

For example, I have user Control and in this user control I have only one richTextBox:

Something like this:

<UserControl x:Class="Spirit.Controls.RichTextBoxControl"
             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:toolkit="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit.Extended" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <toolkit:RichTextBox Name="RichTextBox" 
                             Grid.Row="0" PreviewKeyDown="?">

       </toolkit:RichTextBox>
    </Grid>
</UserControl>

I use this control in WPF window.

<Window x:Class="WpfApplication2.Window2"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:Controls="clr-namespace:WpfApplication2.Controls" xmlns:WpfApplication2="clr-namespace:WpfApplication2" Title="Window2" Height="300" Width="300">
    <Grid>

        <Spirit.Controls:RichTextBoxControl Background="Red"
                                            FontSize="13"
                                            Margin="4,4,4,4" 
                                            Grid.Row="0" 
                                            Here I would like to acces to PreviewKeyDown of richTextBox/>
    </Grid>
</Window>

I would like have access to PreviewKeyDown of richTextBox, bind some method on this event and have access to KeyEventArgs.

Something like this:

   private void RichTextBoxInUserControl_PreviewKeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Enter )
        {
         //...
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜味拾荒者 2024-10-18 12:46:15

我注意到 Intellisence 没有在 Window 中的 RichTextBox... 上识别,但您可以像这样订阅该事件

<Spirit.Controls:RichTextBoxControl
               Name="RichTextBoxInUserControl"
               Background="Red"
               FontSize="13"
               Margin="4,4,4,4"
               Grid.Row="0"
               RichTextBox.PreviewKeyDown="RichTextBoxInUserControl_PreviewKeyDown"/>

,其中 RichTextBox工具包的名称:RichTextBox 在您的 UserControl 中指定

I noticed the Intellisence wasn't picking up on RichTextBox... in Window but you can subscribe to that event like this

<Spirit.Controls:RichTextBoxControl
               Name="RichTextBoxInUserControl"
               Background="Red"
               FontSize="13"
               Margin="4,4,4,4"
               Grid.Row="0"
               RichTextBox.PreviewKeyDown="RichTextBoxInUserControl_PreviewKeyDown"/>

where RichTextBox is the Name of the toolkit:RichTextBox specified in your UserControl

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文