2013-04-26 54 views
1

我有一個自定義控件,它顯示一些統計數據,並且總是需要放置在WP7屏幕的頂部邊緣。但是,當用戶在文本框上輸入某些內容時,軟鍵盤彈出。自定義控件移出屏幕。我想確保自定義控件始終可見,即使在彈出軟鍵盤時也是如此。有誰知道如何做到這一點?如何「粘貼」屏幕頂部的控件?

回答

2

你必須使用一些「魔法」。 「魔術」我的意思是RenderTransform

解決方法很簡單 - 您需要移動自定義控件(向下,當鍵盤可見時;向上,當隱藏時)。
檢查this valuable post - 它必須幫助你。

問候。

+0

謝謝,它的工作原理。 – 2013-04-27 14:00:20

2
<phone:PhoneApplicationPage 
    x:Class="Test.Keyboard.MainPage" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" 
    FontFamily="{StaticResource PhoneFontFamilyNormal}" 
    FontSize="{StaticResource PhoneFontSizeNormal}" 
    Foreground="{StaticResource PhoneForegroundBrush}" 
    SupportedOrientations="PortraitOrLandscape" 
    > 
    <Grid x:Name="LayoutRoot" > 
     <Grid.RowDefinitions> 
      <RowDefinition Height="Auto"/> 
      <RowDefinition Height="*"/> 
      <RowDefinition Height="Auto"/> 
     </Grid.RowDefinitions> 
     <StackPanel Grid.Row="0" Margin="12,17,0,28"> 
      <TextBlock Text="WINDOWS PHONE" Style="{StaticResource PhoneTextNormalStyle}"/> 
      <TextBlock Text="developer's ?" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/> 
     </StackPanel> 
     <Grid Grid.Row="1" Margin="12,0,12,0"></Grid> 
     <TextBox Grid.Row="2" LostFocus="TextBoxLostFocus"/> 
    </Grid> 
</phone:PhoneApplicationPage> 




public partial class MainPage : PhoneApplicationPage 
    { 
     private const double LandscapeShift = -259d; 
     private const double LandscapeShiftWithBar = -328d; 
     private const double Epsilon = 0.00000001d; 
     private const double PortraitShift = -339d; 
     private const double PortraitShiftWithBar = -408d; 

     public static readonly DependencyProperty TranslateYProperty = DependencyProperty.Register("TranslateY", typeof(double), typeof(MainPage), new PropertyMetadata(0d, OnRenderXPropertyChanged)); 

     public MainPage() 
     { 
      InitializeComponent(); 
      Loaded += MainPageLoaded; 
     } 

     public double TranslateY 
     { 
      get { return (double)GetValue(TranslateYProperty); } 
      set { SetValue(TranslateYProperty, value); } 
     } 

     private static void OnRenderXPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
     { 
      ((MainPage)d).UpdateTopMargin((double)e.NewValue); 
     } 

     private void MainPageLoaded(object sender, RoutedEventArgs e) 
     { 
      BindToKeyboardFocus(); 
     } 

     private void BindToKeyboardFocus() 
     { 
      PhoneApplicationFrame frame = Application.Current.RootVisual as PhoneApplicationFrame; 
      if (frame != null) 
      { 
       var group = frame.RenderTransform as TransformGroup; 
       if (group != null) 
       { 
        var translate = group.Children[0] as TranslateTransform; 
        var translateYBinding = new Binding("Y"); 
        translateYBinding.Source = translate; 
        SetBinding(TranslateYProperty, translateYBinding); 
       } 
      } 
     } 

     private void UpdateTopMargin(double translateY) 
     { 
      if (IsClose(translateY, LandscapeShift) || IsClose(translateY, PortraitShift) 
||IsClose(translateY, LandscapeShiftWithBar) || IsClose(translateY, PortraitShiftWithBar) 
) 
      { 
       LayoutRoot.Margin = new Thickness(0, -translateY, 0, 0); 
      } 
     } 

     private bool IsClose(double a, double b) 
     { 
      return Math.Abs(a - b) < Epsilon; 
     } 

     private void TextBoxLostFocus(object sender, RoutedEventArgs e) 
     { 
      LayoutRoot.Margin = new Thickness(); 
     } 
    } 

好運....

+0

謝謝,更改一些代碼後工作。 – 2013-04-27 14:01:16