即使内容高于容器,WPF控件也会对齐底部

时间:2014-06-14 03:33:19

标签: c# wpf xaml wpf-controls

我有一个需要显示的控件,以便它始终在其容器的底部对齐,即使控件高于其容器。我设置了控件的VerticalAlignment="Bottom",当容器较大时工作正常,但当容器收缩时,控件的顶部与容器的顶部对齐,底部被切断。我需要顶部切断。

<Window x:Class="NestedListBox.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:system="clr-namespace:System;assembly=mscorlib"
        x:Name="Root"
        Title="MainWindow" Height="185" Width="525">
    <Grid>
        <ItemsControl VerticalAlignment="Bottom">
            <system:String>Test 1</system:String>
            <system:String>Test 2</system:String>
            <system:String>Test 3</system:String>
            <system:String>Test 4</system:String>
            <system:String>Test 5</system:String>
            <system:String>Test 6</system:String>
            <system:String>Test 7</system:String>
            <system:String>Test 8</system:String>
            <system:String>Test 9</system:String>
            <system:String>Test 10</system:String>
            <system:String>Test 1</system:String>
            <system:String>Test 2</system:String>
            <system:String>Test 3</system:String>
            <system:String>Test 4</system:String>
            <system:String>Test 5</system:String>
            <system:String>Test 6</system:String>
            <system:String>Test 7</system:String>
            <system:String>Test 8</system:String>
            <system:String>Test 9</system:String>
            <system:String>Test 10</system:String>
        </ItemsControl>
    </Grid>
</Window>

当您拖动窗口大于ItemsControl时,它会保持与底部对齐,但只要窗口变小,顶部就会对齐。

我还尝试Grid设置DockPanel而不是ItemsControl,而不是DockPanel.Dock="Bottom",但这也不起作用。

1 个答案:

答案 0 :(得分:4)

您可以使用Canvas来实现预期效果

请注意,VerticalAlignment已从ItemsControl中删除,Canvas.Bottom设置为0,以使其在画布中保持底部。

<Grid>
    <Canvas>
        <ItemsControl Canvas.Bottom="0">
            <system:String>Test 1</system:String>
            <system:String>Test 2</system:String>
            <system:String>Test 3</system:String>
            <system:String>Test 4</system:String>
            <system:String>Test 5</system:String>
            <system:String>Test 6</system:String>
            <system:String>Test 7</system:String>
            <system:String>Test 8</system:String>
            <system:String>Test 9</system:String>
            <system:String>Test 10</system:String>
            <system:String>Test 1</system:String>
            <system:String>Test 2</system:String>
            <system:String>Test 3</system:String>
            <system:String>Test 4</system:String>
            <system:String>Test 5</system:String>
            <system:String>Test 6</system:String>
            <system:String>Test 7</system:String>
            <system:String>Test 8</system:String>
            <system:String>Test 9</system:String>
            <system:String>Test 10</system:String>
        </ItemsControl>
    </Canvas>
</Grid>