10

新しいものを(画像付きで)追加するウィンドウがあります。UserControlコントロールを画面の中央(垂直方向と水平方向の両方)に配置したいだけです。私は垂直のものしか動作させられません。CodeBehindのコンテンツをスワップしDockPanel、スライドショー UI を開始する前にこの起動画面を表示したいと考えています。これは、コンテンツが CodeBehind から設定されていることを意味します。

私のWindow

<Window x:Class="GreenWebPlayerWPF.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="512" Width="853" WindowStyle="None" WindowState="Maximized" WindowStartupLocation="CenterScreen">
    <DockPanel Width="Auto" Height="Auto" Name="TransitionContainer" Background="Black" Margin="0" LastChildFill="True"></DockPanel>
</Window>

私のUserControl

<UserControl x:Class="GreenWebPlayerWPF.FrontPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <DockPanel Background="Black">
        <Image Name="image1" Stretch="None" Source="/GreenWebPlayerWPF;component/gw.png" />
    </DockPanel>
</UserControl>

最大化/フルスクリーンを使用していることに注意してください。

4

3 に答える 3

18

次を使用しGridます。

  <Grid>  
    <Grid.ColumnDefinitions>
      <ColumnDefinition Width="*"/>
      <ColumnDefinition Width="Auto"/>
      <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
      <RowDefinition Height="*"/>
      <RowDefinition Height="Auto"/>
      <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <!-- Replace with your UserControl -->
    <Button Content="Foo" Grid.Column="1" Grid.Row="1"/>
  </Grid>

DockPanel(そこにある必要がある場合)内部にドッキングしDockPanelて伸ばすことができます。もちろん、上記はすべてマークアップですが、コードからこのようなグリッドを簡単に作成できます。

于 2009-08-21T20:05:00.490 に答える
3

ページの要素を中央に配置しようとすると、この問題が発生し続けます。StackPanel の問題は、Orientation が Horizo​​ntal の場合に Horizo​​ntalAlignment が無効になり、Orientation が Vertical の場合に VerticalAlignment が無効になることです。そのため、効果のない値を設定しようとして頭を叩き続けます。このように動作することは非論理的ではありませんが、これがエラーとして報告されるとよいでしょう。

私が見つけた解決策は、以下に示すように、1 つを水平方向に中央に配置し、もう 1 つを垂直方向に配置することです。中間パネルのサイズを変更するには、親のサイズを見つける必要があります。そうしないと、中間パネルがフラットになり、そのコンテンツが非表示になります。絶対値も機能します。万能薬ではありませんが、グリッドを使用するよりも少し冗長です。

<StackPanel Background="Bisque" Orientation="Vertical" Width="300" Height="300" >
    <StackPanel HorizontalAlignment="Center"  Orientation="Horizontal"
                  Height="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=StackPanel}, Path=ActualHeight}">
        <StackPanel VerticalAlignment="Center" Width="200" Height="60" Background="Blue">
        </StackPanel>
    </StackPanel>
</StackPanel>
于 2013-04-24T06:54:13.153 に答える