开发者

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 :

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.

Heres a sample of the content of this '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 开发者_JAVA技巧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. Subsequent scrollviewers (in new windows) then work correctly...

I haven't altered any styles at all for the listbox or the scrollviewer... Very confused!

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


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 :

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

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

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


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.

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!

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜