English 中文(简体)
列表框无滚动条
原标题:Listbox No Scrollbar
  • 时间:2012-05-22 11:22:38
  •  标签:
  • c#
  • wpf
  • xaml

I have been up and down looking on the internet and many people seem to have this problem but its generally solved by changing the container to a grid, constraining the height etc. etc. I can t to seem to get this to work. I have an observableCollection thats feeding into a DataTemplate. I can t for th life of me get the scrollbar working. Its there but not enabling. Thanks 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>

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

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

这是从另一个工程复制过来的 。 完全混淆了 。 请不要担心。 这不是一个逻辑问题。 请将此视为一个密钥, 没有解决方案 。 再次向 Scott 干杯 。

我不清楚为什么会发生这种事,但这里就是了。

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

        }
        else
            Keyboard.Focus(this.txtUserName);

If I clear the comments on LoginOK procedure I will get no scrollbar. With it commented and I interact with the login screen then everything is fine. Finally here is the code for the 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();
            }
问题回答

请提供数据模板,因为我相信我刚刚设法 模拟了你们想要实现的目标, 并且我看到滚动条, 并且开始工作。

以下是我用于 ListBox 数据模板的 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>

This will work with any items control as stated in this article: http://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.itemcontainerstyle.aspx

最后我建议的最后一件事 就是为 ListBox 创建您自己的控制模板, 像这样 :

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




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签