[英]WPF KeyDown does not fire in StackPanel
There are StackPanels in Listbox. 列表框中有StackPanels。 MouseDown does work, but KeyDown - doesn't.
MouseDown可以工作,但KeyDown-不能。 KeyDown works in Listbox.
KeyDown可在列表框中使用。
[EDIT: suggestion about "visible" didn't work for me. [编辑:关于“可见”的建议对我不起作用。 I post more code, maybe the problem is connected with other circumstances in the code]
我发布了更多代码,也许问题与代码中的其他情况有关]
<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>
If your StackPanel
needs to receive keyboard inputs, it must first receive the keyboard focus. 如果您的
StackPanel
需要接收键盘输入,则它必须首先接收键盘焦点。
Then, from MSDN , in order for a StackPanel
to receive keyboard focus, it must be focusable ( Focusable = true
) and visible ( IsVisible = true
). 然后,从MSDN中 ,为了使
StackPanel
能够接收键盘焦点,它必须是可聚焦的 ( Focusable = true
)和可见的 ( IsVisible = true
)。 By default, panels are not focusable, so you need to manually set it to focusable. 默认情况下,面板不可聚焦,因此您需要手动将其设置为可聚焦。
<ListBox>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Focusable="True"
MouseDown="mouseDownPanel" KeyDown="keydownonpanel">
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.