Reputation: 6953
I have previously added a "close" button to a sip by setting InputScope to Search, handling the key up event and calling Focus if the Key is Enter.
I tried to do the same thing in a user control containing a textblock and a textbox and the sip just won't close.
Here is the user control:
XAML
<UserControl
x:Class="SlidePanels.UserControls.TextBoxControl"
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"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
d:DesignWidth="480">
<StackPanel
Orientation="Vertical"
Background="{StaticResource PhoneChromeBrush}">
<TextBlock
x:Name="LabelControl"
Text="Label Control"
Style="{StaticResource PhoneTextNormalStyle}" />
<TextBox
x:Name="TextControl"
Text="Text Control"
InputScope="Search"
KeyUp="TextControl_KeyUp" />
</StackPanel>
Code:
using System.Windows.Input;
namespace SlidePanels.UserControls
{
public partial class TextBoxControl
{
public TextBoxControl()
{
InitializeComponent();
}
public string FieldName { get; set; }
public string Label
{
set { LabelControl.Text = value; }
}
public string Text
{
get { return TextControl.Text; }
set { TextControl.Text = value; }
}
private void TextControl_KeyUp(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
Focus();
}
}
}
}
Any ideas what I'm doing wrong?
Upvotes: 0
Views: 1156
Reputation: 109119
This is what I did in one of my UserControl
s to close the SIP:
private static T FindParent<T>( UIElement control ) where T : UIElement
{
UIElement p = VisualTreeHelper.GetParent( control ) as UIElement;
if( p != null ) {
if( p is T ) {
return p as T;
} else {
return FindParent<T>( p );
}
}
return null;
}
// Loaded callback for the UserControl
private void OnUserControlLoaded( object sender, RoutedEventArgs e )
{
_parentPage = FindParent<PhoneApplicationPage>( this );
}
private void OnTextBoxKeyUp( object sender, KeyEventArgs e )
{
if( e.Key == Key.Enter ) {
if( _parentPage != null ) {
_parentPage.Focus();
}
}
}
Upvotes: 1
Reputation: 14882
You should be able to get this working by calling Focus() on a control that will accept focus other than the TextBox. Making something like a button not visible can be used if you don't already have something else suitable.
Upvotes: 2