2012-12-16 20 views
5

次のコードを使用してポップアップを作成しましたが、中央に配置する方法がわかりません
実行時に自動的に余白を変更しようとしましたが、誰かがポップアップをどのように中央に置くかという考えを持っていますか?
それはそれが言うように、私は以下のuser1603313の答えを試みたが、それはトリックをしなかった私は私のプログラムXAMLの中央ポップアップ

<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}" Name="MainGrid"> 
    <Popup x:Uid="LoginPopup" IsOpen="True" Name="LoginPopup"> 
     <Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="Auto" /> 
      <RowDefinition Height="Auto" /> 
      <RowDefinition Height="Auto"/> 
      <RowDefinition Height="Auto" /> 
     </Grid.RowDefinitions> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="Auto" /> 
      <ColumnDefinition Width="*" /> 
     </Grid.ColumnDefinitions> 
     <TextBlock Margin="10" Grid.Column="0" Grid.Row="0" Text="App Name" Grid.ColumnSpan="2" Style="{StaticResource HeaderTextStyle}" /> 
     <TextBlock Margin="10" Grid.Column="0" Grid.Row="1" Text="Username" Style="{StaticResource ResourceKey=SubheaderTextStyle}" /> 
     <TextBox Margin="10" Grid.Column="1" Grid.Row="1" Name="InputUsername" /> 
     <TextBlock Margin="10" Grid.Column="0" Grid.Row="2" Text="Password" Style="{StaticResource ResourceKey=SubheaderTextStyle}" /> 
     <PasswordBox Margin="10" Grid.Column="1" Grid.Row="2" Name="InputPassword" /> 
     <StackPanel Margin="10" Grid.Column="1" Grid.Row="3" HorizontalAlignment="Left" Orientation="Horizontal"> 
      <Button Name="Login" x:Uid="LoginPopupLogin" /> 
      <Button Name="Cancel" x:Uid="LoginPopupCancel" /> 
     </StackPanel> 
     </Grid> 
    </Popup> 
</Grid> 

UPDATE

をグローバル化する必要があり、標準的な寸法原因がありませんポップアップ内のグリッドのサイズはNaNです。
は、私はまた、グリッドにメソッドを移動しようとしたが、それはトリックをしなかったのどちらか、私が話しています方法はここ

private void LoginPopup_Loaded_1(object sender, RoutedEventArgs e) 
{ 
    LoginPopup.HorizontalOffset = (Window.Current.Bounds.Width - gdChild.ActualWidth)/2; 
    LoginPopup.VerticalOffset = (Window.Current.Bounds.Height - gdChild.ActualHeight)/2; 
} 
+0

にそのメソッドを呼び出すことができているあなたは、フライアウトと思われます。 http://msdn.microsoft.com/en-us/library/windows/apps/hh465354.aspx –

+0

@SinanErgin私が読んだように、フライアウトはHTML – The87Boy

+0

ああのためだけです、それは本当です。フライアウトは使用できませんxaml + c#/ vb。良い仕事;) –

答えて

8

正しく更新グリッドでこれをある
あなたの問題への解決策です。私はxamlコードを変更して書き直しています。コードの後に​​説明があります。ここで

 <Popup x:Uid="LoginPopup" IsOpen="True" Name="LoginPopup" Loaded="LoginPopup_Loaded_1"> 
     <Grid Background="Red" x:Name="gdChild" Height="Auto" Width="Auto"> 
      <Grid.RowDefinitions> 
       <RowDefinition Height="Auto" /> 
       <RowDefinition Height="Auto" /> 
       <RowDefinition Height="Auto"/> 
       <RowDefinition Height="Auto" /> 
      </Grid.RowDefinitions> 
      <Grid.ColumnDefinitions> 
       <ColumnDefinition Width="Auto" /> 
       <ColumnDefinition Width="*" /> 
      </Grid.ColumnDefinitions> 
      <TextBlock Margin="10" Grid.Column="0" Grid.Row="0" Text="App Name" Grid.ColumnSpan="2" Style="{StaticResource HeaderTextStyle}" /> 
      <TextBlock Margin="10" Grid.Column="0" Grid.Row="1" Text="Username" Style="{StaticResource ResourceKey=SubheaderTextStyle}" /> 
      <TextBox Margin="10" Grid.Column="1" Grid.Row="1" Name="InputUsername" /> 
      <TextBlock Margin="10" Grid.Column="0" Grid.Row="2" Text="Password" Style="{StaticResource ResourceKey=SubheaderTextStyle}" /> 
      <PasswordBox Margin="10" Grid.Column="1" Grid.Row="2" Name="InputPassword" /> 
      <StackPanel Margin="10" Grid.Column="1" Grid.Row="3" HorizontalAlignment="Left" Orientation="Horizontal"> 
       <Button Name="Login" x:Uid="LoginPopupLogin" /> 
       <Button Name="Cancel" x:Uid="LoginPopupCancel" /> 
      </StackPanel> 
     </Grid> 
    </Popup> 

私はC#でイベントコードは、ポップアップここ

のXAMLにイベントLoaded="LoginPopup_Loaded_1"します追加した説明

private void LoginPopup_Loaded_1(object sender, RoutedEventArgs e) 
    { 
     LoginPopup.HorizontalOffset = (Window.Current.Bounds.Width - gdChild.ActualWidth)/2; 
     LoginPopup.VerticalOffset = (Window.Current.Bounds.Height - gdChild.ActualHeight)/2; 
    } 

Horizo​​ntalOffsetは、左の間の距離を取得しますアプリケーションウィンドウの左側とポップアップの左側に表示されます。我々は幅と幅からポップアップの高さの半分を減算しなければならないので、それを整列中央に持っているので、同様に垂直オフセット

ポップアップ

のウィンドウの上部と上部との間の距離を取得しますアプリケーションウィンドウの高さ(ポップアップの中心はポップアップの上端と左端の境界からポップアップまでの距離の半分です)

このイベントは、アプリケーションでレンダリングされるときに呼び出されるため、イベントはLoaded="LoginPopup_Loaded_1"イベントで記述されます。ウィンドウとグリッドは、すべての子要素のコンテナグリッドであるために取得されます。私は願っています

は約x

+0

これは実際に私が探していたものでしたが、私はLoadedについては考えていませんでした;)x:NameとNameの違いを教えてください。私は、グリッドの高さと幅がNaNであることを伝えているので、私は素早く行っていたようです。 – The87Boy

+0

グリッドのサイズはどうですか? – The87Boy

+0

画面が回転しているときにLoadedを実行することは可能ですか? – The87Boy

0

:)明らかメートル:名および属性名のuは私は下のリンクで書かれたものと同じになります説明します尋ね

x:name and name difference silverlight

も名前はAPがある私の一般的な理解を1としてWPF

In WPF, what are the differences between the x:Name and Name attributes?

のために、このリンクを通過クラス(例:GridまたはTextBlockなど)の名前の属性を持たないクラスには、その目的のためにhow(例:StoryBoardなど)にアクセスする必要があります。 initializecomponentメソッド呼び出し中に、x:nameと名前は、FindName(string)メソッドを使用してクラスにマップされ、アクセス可能になります。とき、そして、それはロードを実行することが可能である:> MainPage.xaml.cs

1

再 - > MainPage.gics -

は現在、内部で何が起こるかMainPage.xamlを短く

をカット長い話です画面が回転していますか? - The87Boy 24分前

これに対する答えはイエス、あなたがメソッドを作成することにより、コードの同じセットを実行し、Window.Current.SizeChanged += Current_SizeChanged;

void Current_SizeChanged(object sender, Windows.UI.Core.WindowSizeChangedEventArgs e) 
    { 
     //call custom method from loaded event as well as size changed event 
    } 
関連する問題