WPF StackPanel布局问题

时间:2010-03-02 04:32:13

标签: c# wpf visual-studio visual-studio-2008

我正在尝试创建类似于此的布局:

alt text http://img20.imageshack.us/img20/3533/stackn.png

这是我的代码:

<StackPanel TextBlock.FontFamily="Segoe UI" Orientation="Horizontal">
    <StackPanel HorizontalAlignment="Stretch" Width="Auto">
        <TextBlock Padding="5,0,5,0" FontSize="12" FontWeight="Bold" Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Title}" />
        <TextBlock Padding="5,0,5,0" FontSize="12" Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Id}" />
    </StackPanel>
    <StackPanel>
        <TextBlock Padding="5,0,5,0" FontSize="10" Text="Delete">
            <TextBlock.Style>
                <Style TargetType="{x:Type TextBlock}">
                    <Style.Triggers>
                        <Trigger Property="IsMouseOver" Value="true">
                            <Setter Property="FontWeight" Value="Bold" />
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </TextBlock.Style>    
        </TextBlock>
        <TextBlock Padding="5,0,5,0" FontSize="10" Text="Move">
            <TextBlock.Style>
                <Style TargetType="{x:Type TextBlock}">
                    <Style.Triggers>
                        <Trigger Property="IsMouseOver" Value="true">
                            <Setter Property="FontWeight" Value="Bold" />
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </TextBlock.Style>    
        </TextBlock>
    </StackPanel>
</StackPanel>

4 个答案:

答案 0 :(得分:6)

为什么不为此使用网格?

<Grid x:Name="LayoutRoot" Background="White">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="300" />
    </Grid.ColumnDefinitions>

    <StackPanel Grid.Column="0">
        <TextBlock Text="{Binding Title}" />
    </StackPanel>

    <StackPanel Grid.Column="1">        
        <TextBlock Text="Move" />
    </StackPanel>

</Grid>

答案 1 :(得分:2)

我认为使用网格作为父元素可能会更好。省略你的风格,什么不是,这是绘图中布局的XAML。

<Grid>
   <Grid.ColumnDefinitions>
      <ColumnDefinition Width="Auto" />
      <ColumnDefinition Width="50" />  <!-- or some other fixed width -->
   </Grid.ColumnDefinitions>

   <StackPanel Grid.Column="0">
     <!-- left hand stackpanel content -->
   </StackPanel>
   <StackPanel Grid.Column="1">
      <!-- right hand StackPanel content -->
   </StackPanel>
</Grid>

答案 2 :(得分:1)

你真的不希望你的红色容器有StackPanel。我将使用DockPanel,将最右侧的蓝色面板停靠在右侧,并确保LastChildFill处于打开状态,以确保最左边的蓝色面板扩展到窗口宽度。

答案 3 :(得分:0)

以下是我从您的帖子中获得的代码:

<DockPanel TextBlock.FontFamily="Segoe UI" LastChildFill="True">
    <StackPanel DockPanel.Dock="Right">
        <TextBlock Padding="5,0,5,0" FontSize="10" Text="Delete">
        <TextBlock.Style>
            <Style TargetType="{x:Type TextBlock}">
                <Style.Triggers>
                    <Trigger Property="IsMouseOver" Value="true">
                        <Setter Property="FontWeight" Value="Bold" />
                    </Trigger>
                </Style.Triggers>
            </Style>
        </TextBlock.Style>
        </TextBlock>
        <TextBlock Padding="5,0,5,0" FontSize="10" Text="Move">
        <TextBlock.Style>
            <Style TargetType="{x:Type TextBlock}">
                <Style.Triggers>
                    <Trigger Property="IsMouseOver" Value="true">
                        <Setter Property="FontWeight" Value="Bold" />
                    </Trigger>
                </Style.Triggers>
            </Style>
        </TextBlock.Style>
        </TextBlock>
    </StackPanel>
    <StackPanel HorizontalAlignment="Stretch" Width="Auto">
        <TextBlock Padding="5,0,5,0" FontSize="12" FontWeight="Bold" Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Title}" />
        <TextBlock Padding="5,0,5,0" FontSize="12" Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Id}" />
    </StackPanel>
</DockPanel>

希望这会有所帮助!!