WPF KeyDown不会在StackPanel中触发

时间:2016-05-08 17:31:45

标签: c# wpf events

列表框中有StackPanels。 MouseDown确实有效,但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>

1 个答案:

答案 0 :(得分:2)

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

然后,从MSDN开始,为了让StackPanel获得键盘焦点,它必须可聚焦Focusable = true)和可见< / strong>(IsVisible = true)。默认情况下,面板不可聚焦,因此您需要手动将其设置为可聚焦。

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