0

ListBoxの高さをStackPanelの高さにバインドして、ListBoxが垂直方向に伸びるようにし、緑色の領域が表示されないようにします。

リストボックスにアイテムがない場合、そのアイテムは非表示になります。アイテムが1より大きい場合、リストボックスはそれ自体を追加/削除ボタンまで拡張する必要があるため、追加/削除ボタンは常にスタックパネルの下部にあります(そのためにドックパネルを使用したくない)

どうやってやるの?拘束力のあるエラーは発生しませんか?

<StackPanel x:Name="stack" Background="Green" DataContext="{Binding DocumentViewModelList/}" Orientation="Vertical" >
    <ListBox SelectionMode="Single" VirtualizingStackPanel.IsVirtualizing="False"
        SelectedItem="{Binding SelectedDocumentViewModel,Mode=TwoWay}"
        Height="{Binding ElementName=stack,Path=Height}"                                               
        Width="Auto"
        Focusable="True"
        ScrollViewer.HorizontalScrollBarVisibility="Auto" 
        ScrollViewer.VerticalScrollBarVisibility="Auto" 
        Grid.Row="1" 
        Name="documentListBox"
        BorderThickness="1"                                                
        ItemsSource="{Binding DocumentList}"
        Visibility="{Binding ElementName=documentListBox,Path=HasItems, Converter={StaticResource boolToVisibilityConverter}}">

        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding Path=Id}" />
                    <TextBlock Text="{Binding Path=Name}" />
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>

        <!--<ListBox.ItemContainerStyle>                                                  
        <Style TargetType="{x:Type ListBoxItem}">
            <Setter Property="IsSelected" Value="{Binding Mode=TwoWay, Path=IsSelected}" />                                                      
        </Style>      
        </ListBox.ItemContainerStyle>-->                                        
    </ListBox>                                                                                      
</StackPanel>

代替テキスト

4

2 に答える 2

0

選択的な高さを実装するには (アイテムが x の場合、または y の場合) 値コンバーターを使用します...また、高さは NaN になると思うので、ActualHeight を試してください (悪い習慣ですが、うまくいくかもしれません)... snoop のようなツールを使用して値を確認します!

スタックパネルを使用している特定の理由はありますか? I グリッドはより適切に機能します (StackPanel は、グリッドが必要なだけのスペースを提供できる場合に必要な最小スペースのみを提供します)?

<Grid>
  <Grid.RowDefinitions>
    <RowDefinition Height="*" />
    <RowDefinition Height="Auto" />
  </Grid.RowDefinitions>
  <ListBox Grid.Row="1" />
  <StackPanel Orientation="Horizontal" Grid.Row="1">
    <!-- Buttons -->
  </StackPanel>
</Grid>
于 2010-09-15T19:51:44.710 に答える
0

StackPanel の代わりに Grid を使用するだけです。

<Grid x:Name="grid" 
      Background="Green" 
      DataContext="{Binding DocumentViewModelList}">

    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>

    <ListBox Grid.Row="0"
             HorizontalAlignment="Stretch"
             VerticalAlignment="Stretch"  ..... />

    <UniformGrid Grid.Row="1"
                 Rows="1"
                 HorizontalAlignment="Center"
                 VerticalAlignment="Bottom">
        <Button Content="Delete" />
        <Button Content="Add" />
        <Button Content="Open" />
    </UniformGrid>

</Grid>

ListBox は単純に最初の行のスペース全体を占有しますが、UniformGrid は必要なスペースだけを使用して一番下の行を占有します (ボーナスとしてすべてのボタンを同じサイズにします)。

ハードコーディングされた幅/高さの値 (または高さ/幅のバインディング) は必要なく、値コンバーターも必要ありません。

于 2010-09-15T20:09:08.730 に答える