列表框没有滚动条

时间:2012-05-22 11:22:39

标签: c# wpf xaml

我一直在互联网上看起来很多人似乎有这个问题,但它通常通过将容器更改为网格,约束高度等来解决。我似乎无法得到这个工作。 我有一个observableCollection,它可以提供给DataTemplate。我不能为我的生活让滚动条工作。它在那里,但没有启用。谢谢Scott

<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>
非常感谢回来。这是我的DataTempate

<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>

一定是个bug。我只有以下......

<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);

如果我清除对LoginOK程序的评论,我将不会得到滚动条。随着它评论,我与登录屏幕交互,然后一切都很好。最后这里是LoginOK的代码。     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();
            }

1 个答案:

答案 0 :(得分:2)

你能为我提供DataTemplate,因为我相信我已经设法模拟你想要实现的目标,并且滚动条可见并且从一开始就可以工作。

这是我用于ListBox的DataTemplate的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>

希望这有帮助。

我还发现了一段时间后我用过的代码,这可能有所帮助。您可以尝试设置ItemsPanelTemplate:

    <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创建自己的ControlTemplate,如下所示:

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