简体   繁体   English

WPF Scrollviewer 不滚动...没有拇指,只响应鼠标滚轮

[英]WPF Scrollviewer not scrolling… No thumb, only responds to mousewheel

I have a WPF.Net 4 application which contains a button that when clicked opens a new windows:我有一个 WPF.Net 4 应用程序,其中包含一个按钮,单击该按钮会打开一个新的 windows:

CreationWindow creationWindow = new CreationWindow();
creationWindow.Owner = this;
CreationWindow.Show();

The window shows fine, but the listbox that it contains (say 100 images as listboxitems for content) does not have a thumb on the scrollbar. window 显示正常,但它包含的列表框(例如 100 个图像作为内容的列表框项)在滚动条上没有拇指。

Heres a sample of the content of this 'CreationWindow'这是此“CreationWindow”内容的示例

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"~
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:Converters="clr-namespace:Krocr.Client.Converters"                  
    x:Class="Krocr.Client.ComparisonMode"
    Title="ComparisonMode" Height="450" Width="700">
  <Grid>
     <ListBox ItemsSource="{Binding Images}"/>
  </Grid>
</Window>

The scrollbar is visible, but I cannot interact with it.滚动条可见,但我无法与之交互。 The mousewheel does scroll the list.鼠标滚轮确实滚动列表。

HOWEVER..然而..

If I add a scrollviewer to my main window, and add some items to it.如果我在我的主 window 中添加一个滚动查看器,并向其中添加一些项目。 Subsequent scrollviewers (in new windows) then work correctly...随后的滚动查看器(在新窗口中)然后正常工作......

I haven't altered any styles at all for the listbox or the scrollviewer... Very confused!我根本没有为列表框或滚动查看器更改任何 styles ......非常困惑!

Help would be greatly appreciated as it's driving me mad.帮助将不胜感激,因为它让我发疯。

EDIT: Added screenshot of problem (cant post images yet as I'm new...)编辑:添加了问题的屏幕截图(由于我是新手,所以还不能发布图片......)

http://i.stack.imgur.com/XdYSs.png http://i.stack.imgur.com/XdYSs.png

It's because a Grid by default doesn't constrain the size of its child controls, and so the ListBox doesn't realize it needs to scroll.这是因为默认情况下 Grid 不限制其子控件的大小,因此 ListBox 没有意识到它需要滚动。

The quickest solution is to replace the Grid with a DockPanel, which will constrain its child, but this may need rethinking if you have more child controls to add later!最快的解决方案是用 DockPanel 替换 Grid,这约束其子控件,但如果您以后要添加更多子控件,则可能需要重新考虑!

I figured it out...我想到了...

It was a crazy visual tree in my mainwindow.xaml, which was breaking the rendering of everything else... Heres the issue:这是我的主窗口中的一棵疯狂的视觉树。xaml,它破坏了其他一切的渲染......这是问题所在:

<Grid Background="#00E5E5E5" Margin="0,75,0,0" Grid.Row="1">
        <Viewbox x:Name="docViewBox" Margin="0">
            <Grid  Margin="5" x:Name="holdingGrid">
                <Canvas x:Name="AggLayer" Margin="0" />

                <Canvas x:Name="rectCanvas"  MouseLeftButtonDown="StartDrag" MouseLeftButtonUp="EndDrag" Background="Transparent"/>
                <ListBox x:Name="overlayCanvas" Background="#00FFFFFF"  IsHitTestVisible="False" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Disabled">
                    <ListBox.ItemsPanel>
                        <ItemsPanelTemplate>
                            <Canvas Width="{Binding ActualWidth, ElementName=rectCanvas}" Height="{Binding ActualHeight, ElementName=rectCanvas}"/>
                        </ItemsPanelTemplate>
                    </ListBox.ItemsPanel>
                </ListBox>

            </Grid>
        </Viewbox>
        <Canvas x:Name="DialogLayer" Margin="0" />
    </Grid>

With that commented, it works fine... Also that xaml completely breaks blend, causing random crazy behaviour...有了评论,它工作正常......另外,xaml 完全打破了混合,导致随机的疯狂行为......

Time to optimize I feel... Thanks for the input:)我觉得是时候优化了...感谢您的输入:)

EDIT: Infact, all I needed to do was remove the Viewbox and things worked fine... very odd EDIT 2: The culprit was the listbox with a canvas itemspanel, this in particular编辑:事实上,我需要做的就是删除 Viewbox 并且一切正常......非常奇怪 编辑 2:罪魁祸首是带有 canvas 项目面板的列表框,尤其是这个

<Canvas Width="{Binding ActualWidth, ElementName=rectCanvas}" Height="{Binding ActualHeight, ElementName=rectCanvas}"/>

Binding those width and height values was causing the viewbox to get into an infinite scaling loop, which was breaking other things.绑定这些宽度和高度值会导致视图框进入无限缩放循环,这会破坏其他东西。 Silly me...傻我...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM