2016-04-19 7 views
2

私は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つのボタンだけではありません。ボタン、アイコン、テキストボックス、ラベルなどです。ウィンドウ全体が乱雑に自動サイズ調整されますが、下部のリストボックスには自動サイズ設定されません)。

答えて

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> 

私はこのケースでは、あなたが望むものを達成し、ボタンとパネルの実際の幅にリストボックスの幅を結合しました。

0

ListBoxの幅を制限する必要があります。それ以外の場合、親ウィンドウは、SizeToContent="WidthAndHeight"のために最もスペースを占めるコントロールのサイズを変更するだけです。例えば、あなたのStackPanelに名前を付け

<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" /> 
0

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

次に、リストボックスとウィンドウのプロパティでこれを追加します。

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

リストボックスに水平スクロールを含める必要があります。

関連する問題