2012-12-16 49 views
5

我已經使用以下代碼創建了一個Popup,但我無法弄清楚如何居中它
我試圖在運行時自動更改邊距,但我無法弄清楚如何去做,但是有沒有人知道如何將彈出窗口居中?
它不具有標準尺寸的原因,我需要全球化,我的程序在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

我下面user1603313的答案試過,但它沒有這樣做的伎倆,因爲它說的彈出窗口內的網格大小爲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當我閱讀時,Flyouts僅適用於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> 

在這裏,我增加了一個事件Loaded="LoginPopup_Loaded_1"在彈出

這裏的XAML是在C#中的事件代碼

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"事件中,因爲在應用程序中呈現該元素時調用此事件窗口和網格被採用,因爲它是所有子元素的容器網格。

我希望中號清晰:)

+0

這實際上是我正在尋找,但我沒有考慮Loaded;)也許你可以告訴我的x:名稱和名稱之間的區別?我似乎很快,因爲它告訴網格的高度和寬度是NaN – The87Boy

+0

網格的大小如何? – The87Boy

+0

當屏幕旋轉時,是否可以運行Loaded? – The87Boy

0

關於x:名稱和名稱屬性ü問我會解釋將是一樣寫在下面的鏈接什麼

x:name and name difference silverlight

,也通過這個鏈接,WPF

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

按我的理解,一般的名稱是AP (例如:Grid或TextBlock等等),但沒有name屬性的類需要訪問一些方法(例如:StoryBoard等),以便爲此提供x:name。在initializecomponent方法調用期間,x:name和name被映射到類,使用FindName(string)方法使它們變得可訪問。

現在什麼內部發生是一個很長的故事剪短

MainPage.xaml文件 - > MainPage.gics - > MainPage.xaml.cs中

1

回覆:而且是它可以運行加載,當屏幕旋轉了嗎? - 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 
    }