将stackpanel停靠在底部,并添加columndefinitions

时间:2014-09-21 10:50:08

标签: wpf xaml

我的问题在图片“wrong.png”中可视化 我创建了一个堆栈面板,它应该有3列,并在底部停靠,以及它的内容,在这种情况下是3个按钮。看看“wrong.png”,这对我来说并不顺利。

我添加了另一张图片“desired.png”,它显示了我想要实现的目标。 感谢您提供的任何帮助。

这是代码:

<Window x:Class="EnerMedit.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="499.88" Width="702">
<DockPanel LastChildFill="True">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />

        </Grid.ColumnDefinitions>

        <Grid.RowDefinitions>
            <RowDefinition Height="auto" />
            <RowDefinition Height="auto" />
            <RowDefinition Height="auto" />
            <RowDefinition Height="auto" />
            <RowDefinition Height="auto" />
            <RowDefinition Height="auto" />
            <RowDefinition Height="auto" />
            <RowDefinition Height="auto" />
            <RowDefinition Height="auto" />
            <RowDefinition Height="auto" />
            <RowDefinition Height="auto" />
        </Grid.RowDefinitions>

        <Label Content="Seavaro" Grid.Row="0" FontSize="18" />

        <Label Content="Date" Grid.Row="1" />
        <DatePicker Grid.Row="2" Name="datepicker" Background="Honeydew" />

        <Label Content="Note:" FontWeight="ExtraBold" Grid.Row="3" Margin="0 10 0 0" />
        <RadioButton Content="Meditation" Name="mediNote" Margin="50 17 0 0" Grid.Row="3" />
        <RadioButton Content="Energy" Name="energyNote" Margin="150 17 0 0" Grid.Row="3" />
        <RadioButton Content="Both" Name="bothNoteButton" Margin="250 17 0 0" Grid.Row="3" Checked="bothNoteButton_Checked" />
        <TextBox Grid.Row="4" Background="Honeydew" />

        <Separator Width="auto" Grid.Row="5" Margin="0 20 0 20" Background="GreenYellow"/>

        <TextBox Grid.Row="6" MinHeight="150" TextWrapping="Wrap" Background="Honeydew" IsReadOnly="True" />
        <CheckBox Content="Done" Name="doneCheck" Grid.Row="7" Margin="5 7 0 0" />

        <Label Content="Akana" Grid.Column="1" Grid.Row="0" FontSize="18" Margin="10 0 0 0" />


        <CheckBox Content="Check all" Grid.Column="1" Grid.Row="1" Margin="200 7 0 0" Name="IsCheckedCheckAll" 
                  Checked="IsCheckedCheckAll_Checked" Unchecked="IsCheckedCheckAll_Checked" />


        <Label Content="Excercise 1" Grid.Column="1" Grid.Row="2"  FontFamily="Comic Sans MS" Margin="10 0 0 0" />
        <CheckBox Name="Exc1" Grid.Column="1" Grid.Row="2" Margin="200 7 0 0" IsChecked="{Binding IsChecked, ElementName=IsCheckedCheckAll, Mode=OneWay}" />

        <Label Content="Excercise 2" Grid.Column="1" Grid.Row="3" FontFamily="Comic Sans MS" Margin="10 0 0 0" />
        <CheckBox Name="Exc2" Grid.Column="1" Grid.Row="3" Margin="200 7 0 0" IsChecked="{Binding IsChecked, ElementName=IsCheckedCheckAll, Mode=OneWay}" />

        <Label Content="Excercise 3" Grid.Column="1" Grid.Row="4" FontFamily="Comic Sans MS" Margin="10 0 0 0" />
        <CheckBox Name="Exc3" Grid.Column="1" Grid.Row="4" Margin="200 7 0 0" IsChecked="{Binding IsChecked, ElementName=IsCheckedCheckAll, Mode=OneWay}" />

        <Label Content="Excercise 4" Grid.Column="1" Grid.Row="5" FontFamily="Comic Sans MS" Margin="10 0 0 0" />
        <CheckBox Name="Exc4" Grid.Column="1" Grid.Row="5" Margin="200 7 0 0" IsChecked="{Binding IsChecked, ElementName=IsCheckedCheckAll, Mode=OneWay}" />

        <Label Content="Excercise 5" Grid.Column="1" Grid.Row="6" FontFamily="Comic Sans MS" Margin="10 0 0 0" />
        <CheckBox Name="Exc5" Grid.Column="1" Grid.Row="6" Margin="200 07 0 0" IsChecked="{Binding IsChecked, ElementName=IsCheckedCheckAll, Mode=OneWay}" />                               
    </Grid>

    <StackPanel VerticalAlignment="Bottom">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>      

                <Button Content="Add" Name="SubmitButton" Grid.Column="0" Height="23" IsEnabled="{Binding ElementName=doneCheck, Path=IsChecked}" />
                <Button Content="Labaci recardo" Name="GetMedButton" Grid.Column="1" Grid.Row="9" />
                <Button Content="Supa recardo" Name="GetEnegButton" Grid.Column="2" />            
        </Grid>
    </StackPanel>
</DockPanel>                

enter image description here enter image description here

1 个答案:

答案 0 :(得分:1)

您需要从DockPanel中移除第二个StackPanel父级,并且还需要交换生成的StackPanelGrid的位置。只有这样,您的主DockPanel才能按照您的预期布局元素。

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="700" Width="800">
    <DockPanel LastChildFill="True">
        <StackPanel DockPanel.Dock="Bottom"/>
        <Grid/>
    </DockPanel>
</Window>