仅自动调整某些控件

时间:2016-04-19 20:42:37

标签: c# wpf autosize

我正在研究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);
    }
}

最初,窗口的大小适合按钮:

enter image description here

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

enter image description here

但我宁愿得到这个:

enter image description here

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

3 个答案:

答案 0 :(得分:1)

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

realm_id

在这里,我使用按钮将ListBox的宽度绑定到面板的实际宽度,在这种情况下可以达到你想要的效果。

答案 1 :(得分:0)

您必须限制import cv2 import numpy as np import math import matplotlib.pyplot as plt def plot_point(point, angle, length, ax): ''' point - Tuple (x, y) coordinates of the pixel angle - orientation angle at (x,y) pixel. length - Length of the line you want to plot. Will plot the line on a 10 x 10 plot. ''' # unpack the first point x, y = point # find the end point endx = x + length endy = length*math.tan(angle)+y ax.plot([x, endx], [y,endy],color='blue') def draw_orientation_map(angles, block_size, center_coordinates, fingerprint_filename, length): img = cv2.imread(fingerprint_filename) row, col = img.shape x_center = y_center = block_size/2#y for rowq and x for columns r,c = angles.shape #note center_coordinates.shape = angles.shape fig = plt.figure() ax = plt.subplot(111) ax.set_ylim([0, row]) # set the bounds to be 10, 10 ax.set_xlim([0, col]) plt.imshow(img, zorder=0, extent=[0,col,0,row]) for i in xrange(0,r): for j in xrange(0,c): plot_point((j*w + y_center, i*w + x_center), angles[i][j], length,ax) plt.show() 的宽度,否则父窗口只会根据ListBox占用最多空格的控件调整大小。

SizeToContent="WidthAndHeight"

答案 2 :(得分:0)

为您的stackPanel命名,例如

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

然后在两个列表框和窗口属性中添加此内容;

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

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

相关问题