ClearFocus在TextBox上

时间:2016-07-06 14:07:48

标签: c# wpf focus

我有一个包含带有自动建议的TextBox的应用程序,如下所示:

    <Grid x:Name="getStringGrid" Grid.Row="1">
        <TextBox x:Name="getStringBox" Text="{Binding Path=GetString, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" KeyDown="contextBox_KeyDown"/>
            <Popup x:Name="getStringPopup"
                   AllowsTransparency="True"
                   Placement="Bottom"
                   IsOpen="{Binding IsFocused, ElementName=getStringBox, Mode=OneWay,UpdateSourceTrigger=PropertyChanged}"
                   PopupAnimation="{DynamicResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}">
                <ListBox SnapsToDevicePixels="{Binding SnapsToDevicePixels}"
                         KeyboardNavigation.DirectionalNavigation="Continue"
                         ItemsSource="{Binding ListString}"
                         VerticalAlignment="Stretch"
                         HorizontalAlignment="Stretch"                             
                         SelectedItem="{Binding Path=GetString}"/>
            </Popup>
        </Grid>

它工作正常,但我关闭弹出窗口getStringPopup有些麻烦。当用户按下返回时,我想更新我的selectedItem并关闭弹出窗口。

我的代码:

private void contextBox_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Return)
    {
        String myString = getStringBox.Text;
        (DataContext as ViewModels.MainViewModel).AddString(myString );
        bool focus = getStringBox.IsFocused;
        Keyboard.ClearFocus();    
        focus = getStringBox.IsFocused;
    }
}

功能Keyboard.ClearFocus()正确重置焦点,但getStringBox.IsFocused仍为True

你能帮助我吗?

0 个答案:

没有答案