私は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のを設定することにより、その最後のスクリーンショットをしましたが、それは完全なアプリケーションのための良い解決策ではありません。完全なアプリケーションでは、 3つのボタンだけではありません。ボタン、アイコン、テキストボックス、ラベルなどです。ウィンドウ全体が乱雑に自動サイズ調整されますが、下部のリストボックスには自動サイズ設定されません)。