Click here to Skip to main content
15,889,116 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, I have a window [^] made up of StackPanels(with background images) arranged in 3 DockPanels(which are contained by another DockPanel). Sometimes if I resize the window(in Visual Studio) and then compile it and run, it looks a bit wrong like this[^]. Why is this happening? How can I solve it?

XML
<Grid>
        <DockPanel LastChildFill="True" HorizontalAlignment="Stretch" Height="Auto">
            <DockPanel Height="31" DockPanel.Dock="Top" VerticalAlignment="Top">
                <StackPanel Height="31" HorizontalAlignment="Left" Name="CornerUpLeft" VerticalAlignment="Top" Width="31" Orientation="Vertical">
                    <StackPanel.Background>
                        <ImageBrush ImageSource="/Hologram;component/Images/HoloCorner.png" TileMode="Tile" Viewport="0,0,31,31" ViewportUnits="Absolute" />
                    </StackPanel.Background>
                </StackPanel>
                <StackPanel Height="31" HorizontalAlignment="Right" Name="CornerUpRight" VerticalAlignment="Top" Width="31" DockPanel.Dock="Right" Margin="0">
                    <StackPanel.Background>
                        <ImageBrush ImageSource="/Hologram;component/Images/HoloXCorner.png" Stretch="Fill" TileMode="Tile" Viewport="0,0,31,31" ViewportUnits="Absolute" />
                    </StackPanel.Background>
                </StackPanel>
                <StackPanel Height="32" Margin="0" Name="XUp" VerticalAlignment="Top" Width="Auto" DockPanel.Dock="Top">
                    <StackPanel.Background>
                        <ImageBrush ImageSource="/Hologram;component/Images/HoloX.png" TileMode="Tile" Viewport="0,0,31,31" ViewportUnits="Absolute" />
                    </StackPanel.Background>
                </StackPanel>
            </DockPanel>
            <DockPanel Name="Bottom" Height="31" Width="Auto" DockPanel.Dock="Bottom" VerticalAlignment="Bottom" LastChildFill="True">
                <StackPanel Height="31" HorizontalAlignment="Left" Margin="0" Name="CornerDownLeft" VerticalAlignment="Center" Width="31">
                    <StackPanel.Background>
                        <ImageBrush ImageSource="/Hologram;component/Images/HoloYCorner.png" Stretch="Fill" TileMode="Tile" Viewport="0,0,31,31" ViewportUnits="Absolute" />
                    </StackPanel.Background>
                </StackPanel>
                <StackPanel Height="31" Margin="0,0,31,0" Name="XDown" VerticalAlignment="Center" DockPanel.Dock="Bottom" Width="Auto">
                    <StackPanel.Background>
                        <ImageBrush ImageSource="/Hologram;component/Images/HoloXF.png" Stretch="Fill" TileMode="Tile" Viewport="0,0,31,31" ViewportUnits="Absolute" />
                    </StackPanel.Background>
                </StackPanel>
                <StackPanel Height="31" HorizontalAlignment="Right" Margin="0,0,0,-31" Name="CornerDownRight" VerticalAlignment="Bottom" Width="31" DockPanel.Dock="Bottom">
                    <StackPanel.Background>
                        <ImageBrush ImageSource="/Hologram;component/Images/HoloXYCorner.png" Stretch="Fill" TileMode="Tile" Viewport="0,0,31,31" ViewportUnits="Absolute" />
                    </StackPanel.Background>
                </StackPanel>
            </DockPanel>
            <DockPanel Height="Auto" Name="dockPanel1" Width="Auto" DockPanel.Dock="Top" LastChildFill="True">
                <StackPanel HorizontalAlignment="Left" Margin="0" Name="YLeft" VerticalAlignment="Stretch" Width="31" Orientation="Vertical" DockPanel.Dock="Left">
                    <StackPanel.Background>
                        <ImageBrush ImageSource="/Hologram;component/Images/HoloY.png" Stretch="Fill" TileMode="Tile" Viewport="0,0,31,31" ViewportUnits="Absolute" />
                    </StackPanel.Background>
                </StackPanel>
                <StackPanel Height="Auto" Name="YRight" Width="31" HorizontalAlignment="Right" Orientation="Horizontal" DockPanel.Dock="Right">
                    <StackPanel.Background>
                        <ImageBrush ImageSource="/Hologram;component/Images/HoloYF.png" Stretch="Fill" TileMode="Tile" Viewport="0,0,31,31" ViewportUnits="Absolute" />
                    </StackPanel.Background>
                </StackPanel>
                <StackPanel Name="Fill" DockPanel.Dock="Top">
                    <StackPanel.Background>
                        <ImageBrush ImageSource="/Hologram;component/Images/HoloFill.png" Stretch="Fill" TileMode="Tile" Viewport="0,0,31,31" ViewportUnits="Absolute" />
                    </StackPanel.Background>
                </StackPanel>
            </DockPanel>
        </DockPanel>
        <Image Name="Close" Stretch="Fill" HorizontalAlignment="Right" VerticalAlignment="Top" Width="34" Height="32" Source="/Hologram;component/Images/HoloButtonXD.png" Margin="0,0,18,0" />
    <Image Height="20" Name="Minimize" Stretch="Fill" Width="28" HorizontalAlignment="Right" VerticalAlignment="Top" Source="/Hologram;component/Images/HoloButtonMinD.png" StretchDirection="Both" Margin="0,13,52,0" />
    </Grid>
Posted
Updated 29-Aug-11 10:20am
v2

1 solution

Never ever trust in the designer whether it's for WPF, Winforms, Swing or whatever.
Without seeing your xaml it's difficult to say what is wrong. So the best I can do is try setting SnapsToDevicePixels="True"

Thanks for the answer but is still doesn't display properly. Any other suggestions?
Yes and sorry if seem I a bit harsh now, but advice that seem harsh is mostly very useful :)
Rewrite your XAML it's just way to complex for the simple effect you want.
1) Don't use StackPanel as a means to display an image it's a layout Panel, as in used to layout UI elements.
2) Use a Grid width 3 columns and 3 rows to make your effect, it's simply and very easy for others to see what you're doing.
3) I'm in a generous mood today, so here is how I would do it, the last two images I don't really understand how fit in so I've left them as they where.
XML
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="31"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="31"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="31"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="31"/>
    </Grid.RowDefinitions>

    <Image Source="/Hologram;component/Images/HoloCorner.png"/>
    <Image Source="/Hologram;component/Images/HoloX.png" Grid.Column="1"/>
    <Image Source="/Hologram;component/Images/HoloXCorner.png" Grid.Column="2"/>

    <Image Source="/Hologram;component/Images/HoloY.png" Grid.Row="1"/>
    <Image Source="/Hologram;component/Images/HoloFill.png" Grid.Column="1" Grid.Row="1"/>
    <Image Source="/Hologram;component/Images/HoloYF.png" Grid.Column="2" Grid.Row="1"/>

    <Image Source="/Hologram;component/Images/HoloYCorner.png" Grid.Row="2"/>
    <Image Source="/Hologram;component/Images/HoloXF.png" Grid.Column="1" Grid.Row="2"/>
    <Image Source="/Hologram;component/Images/HoloXYCorner.png" Grid.Column="2"
           Grid.Row="2"/>

    <Image Name="Close" Stretch="Fill" HorizontalAlignment="Right" VerticalAlignment="Top"
           Width="34" Height="32" Source="/Hologram;component/Images/HoloButtonXD.png" 
           Margin="0,0,18,0" />
    <Image Height="20" Name="Minimize" Stretch="Fill" Width="28"
           HorizontalAlignment="Right" VerticalAlignment="Top"
           Source="/Hologram;component/Images/HoloButtonMinD.png" StretchDirection="Both"
           Margin="0,13,52,0" />

</Grid>



This might very well solve your problem, but no garentees. make the change and then if you're still having problems, then lets take it from there :D

Just updated the XAML I had messed up Grid.Row on some of the elements :^)
 
Share this answer
 
v8
Comments
mostwanted4 29-Aug-11 16:21pm    
Thanks for the answer but is still doesn't display properly. Any other suggestions?
mostwanted4 30-Aug-11 5:28am    
Your code worked fine but i made some adjustments: instead of <Image> I used <rectangle> because I wanted the image to be repeated not stretched. Now it works just fine.
Simon Bang Terkildsen 30-Aug-11 5:32am    
Ah yeah, I didn't consider the tiling, you should use a Rectangle or Border to use your brush instead of a StackPanel

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