开发者

WPF height and scroll at listbox - imposible

开发者 https://www.devze.com 2023-04-08 22:49 出处:网络
To keep things simple all i am trying to do is to place a listbox and a button below it. Although it seems simple it isn\'t.It is not, because it is in a GRID...

To keep things simple all i am trying to do is to place a listbox and a button below it. Although it seems simple it isn't. It is not, because it is in a GRID...

The row size is star size and listbox having verticalAlign top works great all the time. The problem is that I cannot add a button below it. Having a grid with 2 rows one star height and the other auto would place the button at the bottom of the form even if listbox contents are little. Trying to set both rows auto doesn't work when resizing as no scrollbar is visible at the listbox.... Any workaround for this??

Update some code

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

       <ListBox DockPanel.Dock="Top" Name="lbStaff"  ItemsSource="{Binding}"  
            Grid.Row="0" VerticalAlignment="Top" BorderThickness="0" 
               Background="WhiteSmoke" Margin="15,10,20,30" Style="{DynamicResource 
               ListBoxUsers}" ScrollViewer.VerticalScrollBarVisibility="Visible">
       <ListBox.Resources>
         <Style  TargetType="{x:Type ListBoxItem}" BasedOn="{StaticResource 
            ListBoxTest1}"></Style>
       </ListBox.Resources>
         <ListBox.ItemTemplate>
             <DataTemplate >
                 <StackPanel Margin="5,4,5,4">
                     <TextBlock   HorizontalAlignment="Stretch"  FontSize="16"><Run 
                     Text="{Binding Name}"/> - <Run Text="{Binding   Mode=OneWay, 
                     Path=PositionString}"/></TextBlock>
                     <TextBlock >Τηλέφωνο <Run Text="{Binding Phone}"/>
                 </TextBlock>
                 </StackPanel>

             </DataTemplate>
           </ListBox.ItemTemplate>
  </ListBox>
     <Button DockPanel.Dock="Top" Grid.Row="0"   HorizontalAlignment="Right" 
       VerticalAlignment="Top" 开发者_Python百科 Margin="0,10,7,0" Style="{DynamicResource 
        ButtonStyleNew1}">
       <Button.Content>
           <StackPanel Orientation="Horizontal">
               <Image Source="/WpfApplication1;component/Images/filenew1.png" 
               Stretch="None" VerticalAlignment="Center"></Image>
               <TextBlock Margin="5,0,0,0" FontSize="16">Προσθήκη Χρήστη</TextBlock>
          </StackPanel>
      </Button.Content>
     </Button>
  </DockPanel>                       

</Grid>

</Grid>


What i usually do is not using rows of grid but using DockPanel you can adjust the alignments by nesting each elements. But again it totally depends on the designer how they design that using grid row or any other way. What i got from the question the following code should help you out. but if you post some code then we can figure out where the actual problem is.

<Grid x:Name="LayoutRoot">
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <DockPanel>
        <DockPanel DockPanel.Dock="Top">
            <ListBox Height="50"/>
        </DockPanel>
        <DockPanel DockPanel.Dock="Bottom">
            <Button Height="20" DockPanel.Dock="Top"/>
            <Label/>
        </DockPanel>
    </DockPanel>
</Grid>


So your problem here is that you have the in the ListBox you have ScrollViewer.VerticalScrollbars=Visible that make it collapsed if you dont want scrollbars and i would suggest not to use margins more in the designing. if you still have scroll bars remove the height of the ListBox and make the window larger. It all depends upon the size of the window if you don't have the size and proper alignment. Refining your code

<Window xmlns:my="clr-
 namespace:System.Windows.Controls;assembly=System.Windows.Controls.Input.Toolkit" 
    x:Class="MyLightWpfApplication.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow"
    xmlns:local="clr-namespace:MyLightWpfApplication.ViewModel"
    DataContext="{Binding Main, Source={StaticResource Locator}}">

<Window.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Skins/MainSkin.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>

</Window.Resources>

<Grid>
    <DockPanel  >
        <DockPanel DockPanel.Dock="Top" Width="400" Height="200">
        <ScrollViewer DockPanel.Dock="Top" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto">
        <ListBox Name="lbStaff"  ItemsSource="{Binding AutoCompleteData}"  
          BorderThickness="0" 
           Background="WhiteSmoke" >

            <ListBox.ItemTemplate>
                <DataTemplate >
                    <StackPanel >
                        <TextBlock   HorizontalAlignment="Stretch"  FontSize="16"><Run 
                 Text="{Binding FirstName}"/> - <Run Text="{Binding   Mode=OneWay, 
                 Path=PositionString}"/></TextBlock>
                        <TextBlock >Τηλέφωνο <Run Text="{Binding LastName}"/>
                        </TextBlock>
                    </StackPanel>

                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
        </ScrollViewer>
        </DockPanel>
        <DockPanel DockPanel.Dock="Bottom">
            <Button DockPanel.Dock="Top" Height="30" Grid.Row="0">
                <Button.Content>
                    <StackPanel Orientation="Horizontal">

                        <TextBlock  FontSize="16">Προσθήκη Χρήστη</TextBlock>
                    </StackPanel>
                </Button.Content>
            </Button>
            <Label/>
        </DockPanel>
    </DockPanel>
</Grid>
</Window>

Its all dependent on how much data you have in the ListBox. I have tried this code populating listbox with 20 rows its not showing scrollbars at all


You might try auto for both and put a MaxHeight on the ListBox but you still take a chance of pushing the button off the screen. Or put both in a ScrollViewer. Or can you live with the button on top?


I solved this problem using a dockpanel. I docked buttons at bottom and then i let listbox to fill the remaining area..(lastchildfill=true) ...

It works fine...

0

精彩评论

暂无评论...
验证码 换一张
取 消