【问题标题】:Autosize to only some controls仅自动调整某些控件的大小
【发布时间】:2016-04-19 20:42:36
【问题描述】:

我正在开发 WPF GUI,我希望窗口根据内容自动调整大小,但不是所有内容:我有一些不同的按钮和其他控件,我想自动调整宽度。如果我在列表框中添加一个长项,我希望窗口保持相同的大小。

示例代码:

<Window x:Class="QuickieWPF.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" SizeToContent="WidthAndHeight" ResizeMode="CanMinimize">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <StackPanel Orientation="Horizontal">
            <Button Width="100" Content="Foo" Click="Button_Click"/>
            <Button Width="100" Content="Bar" Click="Button_Click"/>
            <Button Width="100" Content="Baz" Click="Button_Click"/>
        </StackPanel>
        <ListBox Grid.Row="1" Name="TheList" Height="100"/>
    </Grid>
</Window>

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

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        string str = "This is a really long text item that will be wider than the " +
            "three buttons. I want a horizontal scrollbar on the listbox, not a wider " +
            "window. I want the window to size to the buttons, not to this listbox.";
        this.TheList.Items.Add(str);
    }
}

最初,窗口大小调整为按钮:

在列表框中添加长项后,我目前得到这个:

但我宁愿得到这个:

(我通过在列表框上设置 MaxWidth 来制作最后一个屏幕截图,但这对于完整的应用程序来说不是一个好的解决方案:在完整的应用程序中,它不仅仅是三个按钮;它是按钮、图标、文本框、标签等,我希望窗口自动调整到整个混乱,而不是底部的列表框。)

【问题讨论】:

    标签: c# wpf autosize


    【解决方案1】:

    您可以将不想自动调整大小的内容的宽度绑定到您所做的内容的实际宽度,例如:

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>
        <StackPanel Orientation="Horizontal" x:Name="panel">
            <Button Width="100"
                    Content="Foo"
                    Click="Button_Click" />
            <Button Width="100"
                    Content="Bar"
                    Click="Button_Click" />
            <Button Width="100"
                    Content="Baz"
                    Click="Button_Click" />
        </StackPanel>
        <ListBox Grid.Row="1"
                 Name="TheList"
                 Height="100" Width="{Binding ElementName=panel, Path=ActualWidth}" />
    </Grid>
    

    这里我将 ListBox 的宽度绑定到带有按钮的面板的实际宽度,在这种情况下达到了您想要的效果。

    【讨论】:

      【解决方案2】:

      您必须限制ListBox 的宽度,否则父窗口只会使用由于SizeToContent="WidthAndHeight" 而占用最多空间的控件来调整大小。

          <StackPanel Orientation="Horizontal">
              <Button Width="100" Content="Foo" Click="Button_Click"/>
              <Button Width="100" Content="Bar" Click="Button_Click"/>
              <Button Width="100" Content="Baz" Click="Button_Click"/>
          </StackPanel>
      
          <!-- set the width to 300 -->
          <ListBox Grid.Row="1" Name="TheList" Height="100" Width="300" />
      

      【讨论】:

        【解决方案3】:

        给你的 stackPanel 起个名字,例如

        <StackPanel Name="MyPanel" Orientation="Horizontal">
        

        然后在你的列表框和窗口属性中添加这个;

        Width="{Binding ElementName=MyPanel, Path=ActualWidth}"
        

        您需要在列表框中包含水平滚动。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-08-29
          • 1970-01-01
          • 1970-01-01
          • 2013-11-30
          • 2019-07-05
          • 1970-01-01
          • 2011-02-13
          相关资源
          最近更新 更多