Click here to Skip to main content
15,878,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi Everyone,
I tried to implement drag-drop application with mvvm in WPF. Everything is ok, but when the out of range, scroll is not appearing. For Example, DropArea is 600x800. If item location is (300,850), the vertical scrollbar is expected to be visible, and the horizontal scrollbar is expected to be invisible. How can I provided that?

Thanks in advance.

XML
<ControlTemplate x:Key="CanvasTemplate">
    <Grid>
        <ScrollViewer x:Name="ScrollViewer" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" >
            <Border removed="{Binding Path=Background,Converter={StaticResource ColorBrushConverter}}">
                <ItemsPresenter x:Name="ItemsPresenter"/>
            </Border>
        </ScrollViewer>
    </Grid>
</ControlTemplate>


<ItemsControl x:Name="ic" DataContext="{Binding Path=CanvasViewModel}" ItemsSource="{Binding Path=Items}" Width="Auto" Height="Auto" VerticalAlignment="Stretch"
            HorizontalAlignment="Stretch" ItemTemplateSelector="{StaticResource ItemDatatemplateSelector}" Template="{StaticResource CanvasTemplate}"
            >
    <ItemsControl.ItemsPanel>
    <ItemsPanelTemplate>
        <Canvas IsItemsHost="True" DataContext="{Binding ElementName=ic, Path=DataContext}" Width="Auto" Height="Auto" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"
                                Background="Transparent" AllowDrop="True"
                                    >
                </Canvas>
    </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>

        <ItemsControl.ItemContainerStyle>
        <Style>
            <Setter Property="Canvas.Left" Value="{Binding Path=Left}"/>
            <Setter Property="Canvas.Top" Value="{Binding Path=Top}"/>
        </Style>
    </ItemsControl.ItemContainerStyle>
</ItemsControl>
Posted
Comments
Leung Yat Chun 26-Nov-13 10:46am    
Canvas do not automatically resize itself when you add items to it, while I searching for solution around I found this:
http://stackoverflow.com/questions/855334/wpf-how-to-make-canvas-auto-resize (take a look to the CanvasAutoSize)
Mahmut Koyuncu 28-Nov-13 9:14am    
Thanks your response and the answer, you are right. It solved my problem.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900