Windows in designer does not correspond to run state.

JerryM

Member
Joined
May 13, 2021
Messages
10
Programming Experience
Beginner
Hello,
I have a smallproblem. The shape of window in WPF in visual designer does not correspond to running state. There is no visible right ScrollBar. Can someone hlep me why ?

Designer:
designer.jpg

Running App:_
run.jpg


And XAMLcode:

C#:
<Window x:Class="WpfApp.MainWindow"
        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:local="clr-namespace:WpfApp"
        mc:Ignorable="d"
        Title="MainWindow" Height="473" Width="800">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="500" />
            <ColumnDefinition Width="300" />
        </Grid.ColumnDefinitions>

        <Grid.RowDefinitions>
            <RowDefinition Height="40" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>

        <DockPanel Grid.Column="0" Grid.Row="0">
            <Menu DockPanel.Dock="Top">
                <MenuItem Header="_File" FontSize="24" FontFamily="Arial">
                    <MenuItem Header="_New" >
                        <MenuItem.Icon>
                            <Image Source="c:\1\WPF-01\Button-Previous-icon.png" />
                        </MenuItem.Icon>
                    </MenuItem>
                    <MenuItem Header="_Open" />
                    <MenuItem Header="_Save" />
                    <Separator />
                    <MenuItem Header="_Exit" />
                </MenuItem>
            </Menu>
        </DockPanel>

        <Button  Grid.Column="0" Grid.Row="1" x:Name="btn001" Content="Hello" Width="200" Height="200" Click="btn001_Click" />

        <TabControl Grid.Column="1" Grid.Row="1" Height="393" Width="300">
            <TabItem Header="TabItem">
                <Grid Background="#FFE5E5E5">
                    <DockPanel>
                        <ScrollViewer VerticalAlignment="Stretch" VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Visible" Width="284">
                            <StackPanel Name="stackPanel1" Width="295">
                                <GroupBox HorizontalAlignment="Left" Header="Text" Height="401" Width="249" Background="White" Canvas.Left="10"/>
                            </StackPanel>
                        </ScrollViewer>
                    </DockPanel>
                </Grid>
            </TabItem>
            <TabItem Header="TabItem">
                <Grid Background="#FFE5E5E5"/>
            </TabItem>
        </TabControl>


    </Grid>
</Window>
 
I see a vertical scrollbar in in that screenshot of the running app. What am I missing about it?
 
That is just a workaround.

Your question was "why is the right scrollbar not visible?" But as shown in your screenshot, it is visible.

The reason why the position is different than you were expecting is because you did not account for the Windows non-client area that is part of the height and width you initially set. By setting the window to size itself to the size of the contents you are working around having to account for the window chrome, and just let WPF do the accounting for you.

Your question is like asking "Why is the sun hot?" and saying that the correct answer is "Go inside where there is air-conditioning." It still doesn't explain why the sun is hot.
 
Back
Top Bottom