Window SizeToContent和ListBox大小调整

时间:2012-06-06 20:48:25

标签: c# wpf xaml

我试图弄清楚如何将WPF列表框与其SizeToContent属性设置为WidthAndHeight的窗口结合使用。 MainWindow有一个包含两行和两列的网格。在右下角,我有一个列表框。我希望窗口中的其余控件控制窗口的大小,而列表框只需填充可用空间。目前,列表框正在扩展以适应其内容,这会导致整个MainWindow扩展。

注意:我试图创建一个简单的示例,但是想指出在我的真实场景中我使用MVVM,并且我想要确定窗口宽度/高度的控件绑定到viewmodel中的属性在加载窗口后设置它们的值。

编辑添加:列表框在我想要确定大小的控件之前绑定到其内容,并且我无法控制它。

以下是MainWindow目前在启动时的样子:

enter image description here

注意红色和蓝色条表示我不想发生的事情。该区域中的内容只能通过滚动条显示。

以下是我希望MainWindow在启动时的样子:

enter image description here

请注意,MainWindow的大小由顶部和左侧的文本块决定,列表框填充可用空间并在必要时使用滚动条。

以下是一些示例代码......

MainWindow.xaml:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" SizeToContent="WidthAndHeight">

<Grid>   
    <Grid.RowDefinitions>
        <RowDefinition Height="auto"/>
        <RowDefinition Height="auto"/>
        <RowDefinition />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="auto"/>
        <ColumnDefinition />
    </Grid.ColumnDefinitions>

    <Grid Grid.Column="1">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="auto"/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>

        <TextBlock Text="The window should fit to the width of this" FontSize="15"/>

        <Canvas Background="Red" Grid.Column="1"/>
    </Grid>

    <Grid Grid.Row="1" Grid.RowSpan="2">
        <Grid.RowDefinitions>
            <RowDefinition Height="auto"/>
            <RowDefinition/>
        </Grid.RowDefinitions>

        <TextBlock Text="The window should fit to the height of this" FontSize="15">
            <TextBlock.LayoutTransform>
                <RotateTransform Angle="-90"/>
            </TextBlock.LayoutTransform>
        </TextBlock>

        <Canvas Background="Blue" Grid.Row="1"/>
    </Grid>

    <Grid Grid.Row="2" Grid.Column="1">
        <ListBox Name="ListBox1">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding}" />
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</Grid>

MainWindow.xaml.cs:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        var Messages = new ObservableCollection<string>() { "This is a long string to demonstrate that the list box content is determining window width" };

        for (int i = 0; i < 16; i++)
            Messages.Add("Test" + i);

        for (int i = 0; i < 4; i++)
            Messages.Add("this text should be visible by vertical scrollbars only");

        ListBox1.ItemsSource = Messages;
    }
}

1 个答案:

答案 0 :(得分:0)

设置列表框ItemsSource,并在加载Window后设置SizeToContent = Manual。

public MainWindow()
{
    InitializeComponent();

    Loaded += OnLoaded;
}

private void OnLoaded(object sender, RoutedEventArgs routedEventArgs)
{
    SizeToContent = SizeToContent.Manual;

    var messages = new ObservableCollection<string>
        {
            "This is a long string to demonstrate that the list" + 
            " box content is determining window width"
        };

    for (int i = 0; i < 16; i++)
    {
        messages.Add("Test" + i);
    }

    for (int i = 0; i < 4; i++)
    {
        messages.Add("this text should be visible by vertical scrollbars only");
    }

    ListBox1.ItemsSource = messages;
}

通过这种方式,主窗口最初的大小适合内容(列表框中没有数据),然后列表框会显示带有滚动条的项目。