如何在WP7用户控件中关闭SIP

时间:2011-01-27 21:47:58

标签: windows-phone-7

我之前通过将InputScope设置为Search,处理key up事件并在Key为Enter时调用Focus来为sip添加一个“关闭”按钮。

我试图在包含文本块和文本框的用户控件中执行相同操作,并且sip不会关闭。

以下是用户控件:

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>

代码:

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();
            }
        }

    }
}

任何想法我做错了什么?

2 个答案:

答案 0 :(得分:2)

你应该能够通过在一个控件上调用Focus()来实现这个功能,该控件将接受除TextBox以外的焦点。如果您还没有其他合适的东西,可以使用不可见的按钮。

答案 1 :(得分:1)

这是我在UserControl之一关闭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();
    }
  }
}
相关问题