列表框无滚动条

本文关键字:滚动条 列表 | 更新日期: 2023-09-27 18:35:01

我一直在互联网上上下寻找,很多人似乎都有这个问题,但通常通过将容器更改为网格,限制高度等来解决。我似乎无法让它工作。我有一个可观察的集合,它正在馈送到数据模板中。我一生都无法让滚动条工作。它在那里,但没有启用。谢谢斯科特

<TabItem Header="select a call" x:Name="TabActiveCalls" Style="{DynamicResource MyTabItem}" FontFamily="QuickType">
            <Grid Margin="0.125,0.412,3.125,0" Height="471" HorizontalAlignment="Stretch">
                <StackPanel Orientation="Horizontal" Width="839.14" HorizontalAlignment="Left" VerticalAlignment="Top" Height="56">
                    <StackPanel Orientation="Vertical" HorizontalAlignment="Left" Margin="0,0,10,0" Width="741.14">
                        <StackPanel Height="28" Orientation="Horizontal" Width="733.14" HorizontalAlignment="Left">
                            <TextBlock x:Name="txtHistoryFound" TextWrapping="Wrap" Text="*" Foreground="#FFE20A0A" Visibility="Collapsed"/>
                            <TextBlock TextWrapping="Wrap" Text="filter by:" Margin="5,0,10,0" Foreground="#FF585AD4" VerticalAlignment="Center" HorizontalAlignment="Center"/>
                            <TextBlock TextWrapping="Wrap" Text="call no" Margin="5,0,2,0" VerticalAlignment="Center" Foreground="#FF5E88DA"/>
                            <TextBox Template="{StaticResource TextBoxBaseControlTemplate}" x:Name="searchCallNo" TextChanged="searchCallNo_TextChanged"  TextWrapping="Wrap" Width="67" Foreground="#FF1341B1" TextAlignment="Center" Margin="5,0,10,0" Height="22" VerticalAlignment="Center"  />
                            <TextBlock TextWrapping="Wrap" Text="postcode" Margin="5,0,2,0" VerticalAlignment="Center" Foreground="#FF5E88DA"/>
                            <TextBox Template="{StaticResource TextBoxBaseControlTemplate}" x:Name="searchPostcode" TextWrapping="Wrap" Width="67" Foreground="#FF1341B1" TextAlignment="Center" Margin="5,0,10,0" Height="22" VerticalAlignment="Center"/>
                            <TextBlock Height="23" x:Name="txtSiteName" FontSize="16" Foreground="#FF0E7C0B" Width="409" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="0" TextAlignment="Right" Text="Airedale International Ltd" />
                        </StackPanel>
                        <StackPanel Width="733.14" HorizontalAlignment="Left">
                            <Border Height="21" Margin="5,6,8,0" CornerRadius="3,3,0,0" BorderThickness="1" BorderBrush="#FFC0BABA">
                                <StackPanel Orientation="Horizontal" Background="#FFD0D5DE">
                                    <TextBlock TextWrapping="Wrap" Text="CALL NO. / DATE DUE/ CUSTOMER" Margin="5,0,0,0" Foreground="{DynamicResource ListTitle}" FontSize="10.667" VerticalAlignment="Center"/>
                                    <TextBlock TextWrapping="Wrap" Text="ENGINEER / ADDRESS" Margin="114,0,0,0" Foreground="{DynamicResource ListTitle}" VerticalAlignment="Center"/>
                                    <TextBlock TextWrapping="Wrap" Text="REPORT" Margin="43,0,0,0" Foreground="{DynamicResource ListTitle}" RenderTransformOrigin="2.543,0.429" VerticalAlignment="Center"/>
                                    <TextBlock TextWrapping="Wrap" Text="CALL" Margin="28,0,0,0" Foreground="{DynamicResource ListTitle}" RenderTransformOrigin="2.543,0.429" VerticalAlignment="Center"/>
                                    <TextBlock TextWrapping="Wrap" Text="POSITION" Margin="43,0,0,0" Foreground="{DynamicResource ListTitle}" RenderTransformOrigin="2.543,0.429" VerticalAlignment="Center"/>
                                </StackPanel>
                            </Border>
                        </StackPanel>
                    </StackPanel>
                    <Image Height="56" Width="90" Source="/ComfortReportEng;component/Media/Images/comfort_group.png"/>
                </StackPanel>
                    <ListBox ItemTemplate="{StaticResource DataTemplateReportList}" ItemsSource="{Binding Source={StaticResource cvsReportList}}" 
                    Margin="5,56,8,0" MaxHeight="415" Height="415" ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.CanContentScroll="True"/>
            </Grid>
        </TabItem>

非常感谢你回来。这是我的数据试探

<DataTemplate x:Key="DataTemplateReportList">
        <Border Margin="0,2,0,0" BorderThickness="1" BorderBrush="#FFA19C9C" CornerRadius="3,3,0,0" Width="810.52" Height="50" >
            <Grid Background="#FF737B89" Height="48" >
                <StackPanel Orientation="Vertical" HorizontalAlignment="Left" Width="274" VerticalAlignment="Top" >
                    <StackPanel Height="23" Orientation="Horizontal" Margin="0">
                        <TextBlock TextWrapping="Wrap" Text="{Binding CallNo, Mode=TwoWay}" Foreground="#FF7DF51E" Margin="5,0,0,0" FontFamily="Verdana"/>
                        <TextBlock TextWrapping="Wrap" Text="{Binding DateDue, Mode=TwoWay, StringFormat=d}" Foreground="White" Margin="5,0,0,0" FontFamily="Verdana"/>
                    </StackPanel>
                    <StackPanel Height="23" Orientation="Horizontal">
                        <TextBlock TextWrapping="Wrap" Text="{Binding CompanyName, Mode=TwoWay}" Foreground="#FFFFEA00" Margin="5,0,0,0" FontFamily="Verdana"/>
                    </StackPanel>
                </StackPanel>
                <StackPanel HorizontalAlignment="Left" Width="456" Orientation="Vertical" Height="46" Margin="274,1,0,1" VerticalAlignment="Top">
                    <StackPanel Height="23" Orientation="Horizontal">
                        <TextBlock TextWrapping="Wrap" Text="{Binding EngineerName, Mode=TwoWay}" Foreground="#FFCAE5C6" Margin="5,0,0,0" FontFamily="Verdana" Width="140"/>
                        <TextBlock TextWrapping="Wrap" Text="{Binding ReportStatus, Mode=TwoWay}" Foreground="#FF7DF51E" Margin="20,0,0,0" FontFamily="Verdana" Width="50"/>
                        <TextBlock TextWrapping="Wrap" Text="{Binding CallStatus, Mode=TwoWay}" Foreground="#FF7DF51E" Margin="20,0,0,0" FontFamily="Verdana" Width="50"/>
                        <TextBlock TextWrapping="Wrap" Text="{Binding Position, Mode=TwoWay}" Foreground="White" Margin="20,0,0,0" FontFamily="Verdana" Width="50"/>
                    </StackPanel>
                    <StackPanel Height="23" Orientation="Horizontal">
                        <TextBlock TextWrapping="Wrap" Text="{Binding Address, Mode=TwoWay}" Foreground="#FFFFEA00" Margin="5,0,0,0" FontFamily="Verdana" Width="483.12"/>
                    </StackPanel>
                </StackPanel>
                <Grid Width="56" HorizontalAlignment="Right" Margin="0,0,12.52,0" VerticalAlignment="Top">
                    <Image Name="imgInfo" Source="/ComfortReportEng;component/Media/Images/Info4.png" Margin="24,8,0,0" Cursor="Hand" MouseLeftButtonDown="imgInfo_MouseLeftButtonDown"/>
                </Grid>
            </Grid>
        </Border>
    </DataTemplate>

一定是一个错误。我只有以下内容..

<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="ComfortReportEng.Views.EngineerReport.EngineerReport"
Title="Engineer Report" WindowStartupLocation="CenterScreen" Loaded="Window_Loaded" SizeToContent="WidthAndHeight" >
<Window.Resources>
    <CollectionViewSource x:Key="cvsReportList"/>
</Window.Resources>
    <Grid Margin="0.125,0.412,3.125,0" Height="471">
        <ListBox Margin="23,64,3,0" Width="834.14"  ItemsSource="{Binding Source={StaticResource cvsReportList}}" 
            x:Name="lbReportList" SelectionChanged="lbReportList_SelectionChanged" Background="#FFEEEFE4" />
    </Grid>

和代码是

 private void LoadReportList()
    {
        int number = 0;
        List<int> myNumbers = new List<int>();
        while (number < 80)
        {
            myNumbers.Add(number);
            number += 1;
        }
        _cvsReportList = (CollectionViewSource)(this.FindResource("cvsReportList"));
        _cvsReportList.Source = myNumbers;
    }

这是从另一个项目复制过来的。完全糊涂了。请不要担心。这不是一个逻辑问题。请将此视为没有解决方案的密切通话。再次干杯斯科特

只是为了给出这种完整性。我不确定为什么会发生这种情况,但就是这样。

if (System.Environment.MachineName == "SCOTT-PC")
        {
            this.txtUserName.Text = "Scott Fisher";
            this.txtPassword.Password = "palace";
            //LoginOK();
        }
        else
            Keyboard.Focus(this.txtUserName);

如果我清除登录确定程序上的评论,我将不会得到滚动条。评论它并与登录屏幕交互,然后一切都很好。最后,这里是登录确定的代码。 if (LoginService.CheckLogin(txtUserName.Text, txtPassword.Password.ToString(((( { Helpers.User.ThisUser = this.txtUserName.Text;

                EngineerReport.EngineerReport engReport = new EngineerReport.EngineerReport()
                {
                    Owner = Window.GetWindow(this),
                    WindowStartupLocation =
                        System.Windows.WindowStartupLocation.
                        CenterOwner
                };
                this.Hide();
                engReport.ShowDialog();
            }
            else
            {
                this.txtPassword.Password = "";
                this.txtPassword.Focus();
            }

列表框无滚动条

你能提供数据模板吗,因为我相信我刚刚设法模拟了你想要实现的目标,并且我有滚动条可见并且从一开始就起作用。

下面是我用于列表框的数据模板的 xaml:

    <ListBox DataContext="{StaticResource MusicData}" ItemsSource="{Binding XPath=Album}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <ListBoxItem>
                    <StackPanel Orientation="Horizontal" >
                        <TextBlock>No.</TextBlock>
                        <TextBlock Text="{Binding XPath=No}"></TextBlock>
                        <TextBlock>Title</TextBlock>
                        <TextBlock Text="{Binding XPath=Title}"></TextBlock>
                    </StackPanel>
                </ListBoxItem>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

希望这有帮助。

我还发现了一段我不久前使用的代码,这可能会有所帮助。您可以尝试设置项目面板模板:

    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel>
                <ScrollViewer VerticalScrollBarVisibility="Visible" />
            </StackPanel>
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>

这将适用于本文中所述的任何项控件:http://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.itemcontainerstyle.aspx

我能建议的最后一件事是为列表框创建自己的控件模板,如下所示:

<ListBox.Template>
    <ControlTemplate>
        <ScrollViewer VerticalScrollBarVisibility="Auto">
            <ItemsPresenter />
        </ScrollViewer>
    </ControlTemplate>
</ListBox.Template>