2011-01-27 7 views
0

InputScopeを検索に設定し、キーアップイベントを処理し、キーが入力されている場合にフォーカスを呼び出すことによって、以前に「閉じる」ボタンをSIPに追加しました。WP7ユーザーコントロールで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(); 
      } 
     } 

    } 
} 

私が間違ってやっている任意のアイデア?

+0

しばしば議論のトピックのようだと私は提案としてこれを投稿します。http://wpdev.uservoice。 com/forums/110705-app-platform/suggestions/1910729-method-to-close-sip – CodeZombie

答えて

2

TextBox以外のフォーカスを受け付けるコントロールでFocus()を呼び出すと、これを実行できるはずです。あなたがまだ他の何かを持っていなければ、ボタンのようなものを目に見えないものにすることができます。

1

これは私がSIPを閉じるために、私のUserControl秒のいずれかにやったことです:

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(); 
    } 
    } 
} 
関連する問題