0

私はこれを簡単に持っていますUserControl

<UserControl x:Class="WPFTreeViewEditing.UserControl1"
             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" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <TextBlock Text="Hello, world!" KeyDown="TextBlock_KeyDown" />
    </Grid>
</UserControl>

TextBlock.KeyDown イベントを処理したい。そのため、コード ビハインドにイベント ハンドラーを追加しました。

public partial class UserControl1 : UserControl
{
    public UserControl1()
    {
        InitializeComponent();
    }

    private void TextBlock_KeyDown(object sender, KeyEventArgs e)
    {
        MessageBox.Show("Key up!");
    }
}

しかし発火しません。どうしたの?

アップデート。

PreviewKeyDownも発火しません。

これUserControlは次の場合に使用されHierarchicalDataTemplateます。

<Window x:Class="WPFTreeViewEditing.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WPFTreeViewEditing"
        Title="MainWindow" Height="265" Width="419">
    <Grid>
        <TreeView ItemsSource="{Binding}">
            <TreeView.Resources>
                <HierarchicalDataTemplate DataType="{x:Type local:ViewModel}" ItemsSource="{Binding Items}">
                    <local:UserControl1 />
                </HierarchicalDataTemplate>
            </TreeView.Resources>
        </TreeView>
    </Grid>
</Window>
4

3 に答える 3

4

UIElement.KeyDownのドキュメントから:

この要素にフォーカスがあるときにキーが押されたときに発生します。

フォーカスのないTextBlockを使用しているため、KeyDownイベントは別のコントロールによって処理されます。

TextBoxに切り替えて、いくつかのスタイルを追加して、TextBlockのように表示および動作するようにすることができますが、フォーカスを取得してイベントを処理することはできます。

于 2012-07-02T09:24:13.023 に答える
0

わかりました、この質問はずっと前に投稿されましたが、同じ問題があり、KeyDown イベントを機能させる方法を見つけましたが、探しているものではないかもしれませんが、コードを投稿して、将来の人々が同じ問題。

まず最初に、Xaml オブジェクトの KeyDown イベント ハンドラーは、そのオブジェクトにフォーカスがある場合にのみ発生します。したがって、CoreWindow イベント ハンドラーが必要です。これは同じことですが、どのオブジェクトまたはものにフォーカスがあっても常に実行されます。以下がコードになります。

//CLASS.xaml.h
ref class CLASS{
private:
    Platform::Agile<Windows::UI::Core::CoreWindow> window;

public:
    void KeyPressed(Windows::UI::Core::CoreWindow^ Window, Windows::UI::Core::KeyEventArgs^ Args);


//CLASS.xaml.cpp
CLASS::CLASS(){
    InitializeComponent();
    window = Window::Current->CoreWindow;

    window->KeyDown += ref new TypedEventHandler
    <Windows::UI::Core::CoreWindow^, Windows::UI::Core::KeyEventArgs^>(this, &CLASS::KeyPressed);
};

void CLASS::KeyPressed(Windows::UI::Core::CoreWindow^ Window, Windows::UI::Core::KeyEventArgs^ Args){
SimpleTextBox->Text = Args->VirtualKey.ToString();
};

基本的に、ウィンドウを保持する値が必要であり、それを使用して新しい TypedEventHandler を作成します。安全のために、通常、クラスのコンストラクターで、クラスが開始された瞬間に一度だけ呼び出される関数でこれを行うことをお勧めします (ただし、コンストラクターを好みます)。

このメソッドを使用して、任意のイベントのイベント ハンドラーを作成できます。KeyUp、PointerMoved、PointerPressed などの別の属性の "KeyDown" を変更し、"&CLASS::KeyPressed" を、対応するタイプのイベントを取得した瞬間に起動する関数の名前に変更するだけです。

于 2013-07-19T01:47:21.020 に答える
0

PreviewKeyDownイベントの代わりにイベントを使用する必要がありますKeyDown

于 2012-07-02T08:31:26.743 に答える