WPF中StackPanel控件下的KeyDown事件无法触发

3
在Listbox中有StackPanels。鼠标按下是可以工作的,但是KeyDown却不行。KeyDown在Listbox中可以工作。
<Window x:Class="PWSG_LAB_4.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:ABCD"
    mc:Ignorable="d"
    Title="Password Manager" Height="500" Width="700" Icon="abc.png"
    Loaded="Window_Loaded">

    <Window.Resources>
        <local:NumberConverter x:Key="konwert"/>
    </Window.Resources>

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="400"/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="180"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>

    <Menu IsMainMenu="True" >
        <MenuItem Header="Main menu">
            <MenuItem Header="Save file" Click="MenuItem_Click" />
            <MenuItem Header="Exit" Click="MenuItem_Click" />
        </MenuItem>
    </Menu>

    <TreeView Name="Drzewo" Grid.Row="1" SelectedItemChanged="SelectionChanged">
        <TreeView.ItemTemplate>
            <HierarchicalDataTemplate DataType="{x:Type local:TreeElement}" ItemsSource="{Binding Items}">
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding Title}" />
                    <TextBlock Text=" [" Foreground="Blue" />
                    <TextBlock Foreground="Blue">
                        <TextBlock.Text>
                            <MultiBinding Converter="{StaticResource konwert}" Mode="OneWay">
                                <Binding ElementName="TreeElementItems.Count"/>
                                <Binding ElementName="hasla.Count"/>
                            </MultiBinding>
                        </TextBlock.Text>
                    </TextBlock>
                    <TextBlock Text="]" Foreground="Blue" />
                </StackPanel>
            </HierarchicalDataTemplate>
        </TreeView.ItemTemplate>
    </TreeView>

    <ListBox Grid.Row="1" Grid.Column="1" Name="listahasel" ItemsSource="{Binding}" AlternationCount="2" >
        <ListBox.ItemTemplate>
            <DataTemplate>

                <StackPanel Name="panelzhaslami" Orientation="Vertical" Height="90" Width="400" Background="Blue" Focusable="True" MouseRightButtonDown="mouseDownPanel"  KeyDown="keydownonpanel">
                    <TextBlock FontSize="20" Text="{Binding placeofwork}" Padding="4"/>
                    <TextBlock FontSize="14" Text="{Binding login}" Padding="4"/>
                    <TextBlock FontSize="14" Text="{Binding password}" Padding="4"/>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

    <ProgressBar Visibility="Hidden" Grid.Row="2" Grid.Column="1" Minimum="0" Height="20" Width="200" Maximum="10" Value="0" Name="Pasek" HorizontalAlignment="Right"/>

</Grid>


这是你的工作代码吗?请分享你目前正在使用的代码。这看起来像是一些不完整的代码。 - ViVi
我们能看到 keydownonpanel 事件吗? - Nejc Galof
StackPanel 应该首先获得键盘焦点。然后,当您按下键时,将触发 StackPanel 的 KeyDown 事件。 - Nejc Galof
1个回答

2

如果您的StackPanel需要接收键盘输入,它必须首先获得键盘焦点。

然后,根据MSDN的说明,为了使StackPanel获得键盘焦点,它必须是可聚焦的Focusable = true)和可见的IsVisible = true)。默认情况下,面板是不可聚焦的,因此您需要手动设置它可聚焦。

<ListBox>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Focusable="True"
                MouseDown="mouseDownPanel"  KeyDown="keydownonpanel">
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

谢谢,但不幸的是它不起作用。我发布了更多的代码。 - Piotr Bródka
你那里有什么可以将焦点分配给 StackPanel 的东西吗?从你捕获 MouseDown 的方式来看,我会认为这是你让 StackPanel 获得焦点的地方。 - Jai

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接