Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Dear all,

In my WPF project I have MainWindow with Scrollviewer in it. Inside the Scrollviewwer is UserControl1 (which inherits from UserControl) that I defined in separate xaml file. That UserControl1 has Rectangle defined in it and that rectangle exceeds dimensions of the UserControl1, namely its lower edge is beneath lower edge of the UserControl1. The rectangle is not clipped by UserControl1 but is by the Scrollviewer.

If this is not clear enough, here is more precise explanation (contents of 2 xaml files which describes my 2 classes MainWindow and UserControl1):

MainWindow.xaml:
XML
<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:clippingIsue1"
    x:Class="clippingIsue1.MainWindow"
    x:Name="Window"
    Title="MainWindow"
    Width="640" Height="480">
    <Grid x:Name="LayoutRoot" Background="White">
        <ScrollViewer x:Name="scv" HorizontalAlignment="Left" Margin="73,108,0,0" Width="242" Background="Silver" Height="70" VerticalAlignment="Top">
            <local:UserControl1 x:Name="uc1" Width="Auto" Height="39" VerticalAlignment="Top"/>
        </ScrollViewer>
    </Grid>
</Window>


UserControl1.xaml
XML
<UserControl
    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"
    mc:Ignorable="d"
    x:Class="clippingIsue1.UserControl1"
    x:Name="UserControl" Width="371.5" Height="30">
    <Grid x:Name="LayoutRoot" Background="#FF9D0404">
        <Rectangle x:Name="rt" Fill="#FF5A5AD2" Stroke="Black" Height="200" Margin="8,8,8,-178"/>
    </Grid>
</UserControl>



My questions are:

1. Why is this happening?
2. What can I do to make my Scrollviewer either not clip the Rectangle, so it exceeds Scrollviewer dimensions, too, or allow scrolling, so that user can scroll down to see the rest of the rectangle?

Thanks in advance.
Posted
Updated 3-Apr-11 14:11pm
v2

I am not sure how it works for you.

The UserControl's Height is define as 30 but the Rectangle's Height is 200. You already messed the Rectangle in the UserControl. Follow this:

1. Set the Height of the UserControl as "Auto" in UserControl.xaml
2. Remove the Height of the Rectangle.
2. Remove the Margin of the Rectangle as Set it as "8,8,8,8"

XML
<UserControl
    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"
    mc:Ignorable="d"
    x:Class="clippingIsue1.UserControl1"
    x:Name="UserControl" Width="371.5" Height="Auto">
    <Grid x:Name="LayoutRoot" Background="#FF9D0404">
        <Rectangle x:Name="rt" Fill="#FF5A5AD2" Stroke="Black" Margin="8,8,8,8"/>
    </Grid>
</UserControl>
 
Share this answer
 
Comments
Tarun.K.S 4-Apr-11 2:16am    
Just as I suspected, good catch!
Venkatesh Mookkan 4-Apr-11 2:20am    
Thanks
dsnlkc 4-Apr-11 3:49am    
OK, I should have been more precise in my question.

What you suggested will definitely make whole rectangle visible but will violate some other conditions of my project. The rectangle will not always be visible. Its Visibility property will be changed in some event handlers and I want Scrollviewer to adjust itself to the change. What I ultimately want to accomplish is to create effect similar to the one when Combobox expands and collapses itself (Combobox can even exceed dimensions of the window). Maybe Rectangle that exceeds UserControl's dimensions isn't the best approach and if you know better one please share it with me.

In any case, thank you for your time.
Venkatesh Mookkan 4-Apr-11 3:53am    
Post a sample application somewhere in www.dropbox.com or skydrive.live.com. Let me know what can I do?
Have you seen Creating A Scrollable Control Surface In WPF[^]?

It sounds as if it might do what you require.
 
Share this answer
 

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