点击背景时不要失去焦点

时间:2011-12-14 19:32:31

标签: .net wpf

我正在我的应用程序中编写一个软键盘作为用户控件。该控件用于在文本框上键入(通过触摸)。我已将每个控件/容器(包括usercontrol本身)设置为focusable =“false”。单击按钮(键)时,我不会失去焦点,但是当我单击用户控件的背景时,焦点将丢失。

我该如何防止这种情况?

XAML代码:

<UserControl x:Class="HardRock_Kiosk_PrototypeScreens.KioskKeyboard"
         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" 
         Height="250" Width="800" Focusable="False" Background="#FF7F7F7F">
<Grid Focusable="False">
    <Grid.RowDefinitions>
        <RowDefinition Height="50" />
        <RowDefinition />
    </Grid.RowDefinitions>
    <StackPanel Orientation="Horizontal" Grid.Row="0" Focusable="False">
        <Button x:Name="HideKeyboardButton" Content="Hide" Height="30" Margin="20,0,0,0" Focusable="False" Style="{DynamicResource Button-Sketch}"/>
        <Button x:Name="PreviousButton" Content="Prev" Height="30" Margin="10,0,0,0" Focusable="False" Style="{DynamicResource Button-Sketch}"/>
        <Button x:Name="NextButton" Content="Next" Height="30"  Focusable="False" Style="{DynamicResource Button-Sketch}"/>
        <Button x:Name="PrintButton" Content="Print" Height="30" Margin="392,0,0,0" Focusable="False" Style="{DynamicResource Button-Sketch}"/>
        <Button x:Name="DoneButton" Content="Done" Height="30" Margin="10,0,0,0" Focusable="False" Style="{DynamicResource Button-Sketch}"/>
    </StackPanel>
    <Grid x:Name="KeysGrid" Grid.Row="1" Focusable="False">
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="600" />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <Grid Grid.Row="0" Grid.Column="0" x:Name="LettersFirstRowGrid" Focusable="False">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="60"/>
                <ColumnDefinition Width="60"/>
                <ColumnDefinition Width="60"/>
                <ColumnDefinition Width="60"/>
                <ColumnDefinition Width="60"/>
                <ColumnDefinition Width="60"/>
                <ColumnDefinition Width="60"/>
                <ColumnDefinition Width="60"/>
                <ColumnDefinition Width="60"/>
                <ColumnDefinition Width="60"/>
            </Grid.ColumnDefinitions>
            <Button x:Name="QButton" Grid.Column="0" Content="Q" Style="{StaticResource KeyLightGray}" />
            <Button x:Name="WButton" Grid.Column="1" Content="W" Style="{StaticResource KeyLightGray}" />
            <Button x:Name="EButton" Grid.Column="2" Content="E" Style="{StaticResource KeyLightGray}" />
            <Button x:Name="RButton" Grid.Column="3" Content="R" Style="{StaticResource KeyLightGray}" />
            <Button x:Name="TButton" Grid.Column="4" Content="T" Style="{StaticResource KeyLightGray}" />
            <Button x:Name="YButton" Grid.Column="5" Content="Y" Style="{StaticResource KeyLightGray}" />
            <Button x:Name="UButton" Grid.Column="6" Content="U" Style="{StaticResource KeyLightGray}" />
            <Button x:Name="IButton" Grid.Column="7" Content="I" Style="{StaticResource KeyLightGray}" />
            <Button x:Name="OButton" Grid.Column="8" Content="O" Style="{StaticResource KeyLightGray}" />
            <Button x:Name="PButton" Grid.Column="9" Content="P" Style="{StaticResource KeyLightGray}" />
        </Grid>
        <Grid Grid.Row="1" Grid.Column="0" x:Name="LettersSecondRowGrid" HorizontalAlignment="Center" Focusable="False">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="60"/>
                <ColumnDefinition Width="60"/>
                <ColumnDefinition Width="60"/>
                <ColumnDefinition Width="60"/>
                <ColumnDefinition Width="60"/>
                <ColumnDefinition Width="60"/>
                <ColumnDefinition Width="60"/>
                <ColumnDefinition Width="60"/>
                <ColumnDefinition Width="60"/>
            </Grid.ColumnDefinitions>
            <Button x:Name="AButton" Grid.Column="0" Content="A" Style="{StaticResource KeyLightGray}" />
            <Button x:Name="SButton" Grid.Column="1" Content="S" Style="{StaticResource KeyLightGray}" />
            <Button x:Name="DButton" Grid.Column="2" Content="D" Style="{StaticResource KeyLightGray}" />
            <Button x:Name="FButton" Grid.Column="3" Content="F" Style="{StaticResource KeyLightGray}" />
            <Button x:Name="GButton" Grid.Column="4" Content="G" Style="{StaticResource KeyLightGray}" />
            <Button x:Name="HButton" Grid.Column="5" Content="H" Style="{StaticResource KeyLightGray}" />
            <Button x:Name="JButton" Grid.Column="6" Content="J" Style="{StaticResource KeyLightGray}" />
            <Button x:Name="KButton" Grid.Column="7" Content="K" Style="{StaticResource KeyLightGray}" />
            <Button x:Name="LButton" Grid.Column="8" Content="L" Style="{StaticResource KeyLightGray}" />
        </Grid>
        <Grid Grid.Row="2" Grid.Column="0" x:Name="LettersThirdRowGrid" HorizontalAlignment="Center" Focusable="False">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="86"/>
                <ColumnDefinition Width="60"/>
                <ColumnDefinition Width="60"/>
                <ColumnDefinition Width="60"/>
                <ColumnDefinition Width="60"/>
                <ColumnDefinition Width="60"/>
                <ColumnDefinition Width="60"/>
                <ColumnDefinition Width="60"/>
                <ColumnDefinition Width="86"/>
            </Grid.ColumnDefinitions>
            <Button x:Name="DotComButton" Grid.Column="0" Content=".com" Style="{DynamicResource KeyDarkGray}" Margin="0,0,10,0" />
            <Button x:Name="ZButton" Grid.Column="1" Content="Z" Style="{StaticResource KeyLightGray}" />
            <Button x:Name="XButton" Grid.Column="2" Content="X" Style="{StaticResource KeyLightGray}" />
            <Button x:Name="CButton" Grid.Column="3" Content="C" Style="{StaticResource KeyLightGray}" />
            <Button x:Name="VButton" Grid.Column="4" Content="V" Style="{StaticResource KeyLightGray}" />
            <Button x:Name="BButton" Grid.Column="5" Content="B" Style="{StaticResource KeyLightGray}" />
            <Button x:Name="NButton" Grid.Column="6" Content="N" Style="{StaticResource KeyLightGray}" />
            <Button x:Name="MButton" Grid.Column="7" Content="M" Style="{StaticResource KeyLightGray}" />
            <Button x:Name="BackSpaceButton" Grid.Column="8" Content="&lt;" Style="{DynamicResource KeyDarkGray}" Margin="10,0,0,0" />
        </Grid>
        <Grid Grid.Row="3" Grid.Column="0" x:Name="LettersFourthRowGrid" HorizontalAlignment="Center" Focusable="False">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="60"/>
                <ColumnDefinition Width="60"/>
                <ColumnDefinition Width="360"/>
                <ColumnDefinition Width="120"/>
            </Grid.ColumnDefinitions>
            <Button x:Name="AtButton" Grid.Column="0" Content="@" Style="{DynamicResource KeyDarkGray}" />
            <Button x:Name="DotButton" Grid.Column="1" Content="." Style="{DynamicResource KeyDarkGray}" />
            <Button x:Name="SpaceButton" Grid.Column="2" Content=" " Style="{StaticResource KeyLightGray}" />
            <Button x:Name="SendGoButton" Grid.Column="3" Content="Send/Go" Style="{DynamicResource KeyBlue}" />
        </Grid>

        <Grid Grid.Row="0" Grid.Column="1" x:Name="NumPadFirstRowGrid" Margin="15,0,0,0" Focusable="False">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="60"/>
                <ColumnDefinition Width="60"/>
                <ColumnDefinition Width="60"/>
            </Grid.ColumnDefinitions>
            <Button x:Name="OneButton" Grid.Column="0" Content="1" Style="{StaticResource KeyLightGray}" />
            <Button x:Name="TwoButton" Grid.Column="1" Content="2" Style="{StaticResource KeyLightGray}" />
            <Button x:Name="ThreeButton" Grid.Column="2" Content="3" Style="{StaticResource KeyLightGray}" />
        </Grid>
        <Grid Grid.Row="1" Grid.Column="1" x:Name="NumPadSecondRowGrid" Margin="15,0,0,0" Focusable="False">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="60"/>
                <ColumnDefinition Width="60"/>
                <ColumnDefinition Width="60"/>
            </Grid.ColumnDefinitions>
            <Button x:Name="FourButton" Grid.Column="0" Content="4" Style="{StaticResource KeyLightGray}" />
            <Button x:Name="FiveButton" Grid.Column="1" Content="5" Style="{StaticResource KeyLightGray}" />
            <Button x:Name="SizButton" Grid.Column="2" Content="6" Style="{StaticResource KeyLightGray}" />
        </Grid>
        <Grid Grid.Row="2" Grid.Column="1" x:Name="NumPadThirdRowGrid" Margin="15,0,0,0" Focusable="False">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="60"/>
                <ColumnDefinition Width="60"/>
                <ColumnDefinition Width="60"/>
            </Grid.ColumnDefinitions>
            <Button x:Name="SevenButton" Grid.Column="0" Content="7" Style="{StaticResource KeyLightGray}" />
            <Button x:Name="EightButton" Grid.Column="1" Content="8" Style="{StaticResource KeyLightGray}" />
            <Button x:Name="NineButton" Grid.Column="2" Content="9" Style="{StaticResource KeyLightGray}" />
        </Grid>
        <Grid Grid.Row="3" Grid.Column="1" x:Name="NumPadFourthRowGrid" Margin="15,0,0,0" Focusable="False">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="60"/>
                <ColumnDefinition Width="120"/>
            </Grid.ColumnDefinitions>
            <Button x:Name="ZeroButton" Grid.Column="0" Content="0" Style="{StaticResource KeyLightGray}" />
            <Button x:Name="EnterButton" Grid.Column="1" Content="Enter" Style="{DynamicResource KeyDarkGray}" />
        </Grid>
    </Grid>
</Grid>

1 个答案:

答案 0 :(得分:0)

显然这是SketchFlow的一个问题,它在正常的WPF应用程序上按预期工作。