开发者

WPF .NET4.0 Content control growing out of screen

Ive got an issue which has been frustrating the heck out of me for a while. The content which is displayed grows out of screen. I.e. no window sizing is applied.

Following is what Ive tried:

<Window ...
        d:DesignHeight="768" d:DesignWidth="1024"
        MinHeight="768" MinWidth="1024"  
        mc:Ignorable="d" SizeToContent="WidthAndHeight"
        WindowStartupLocation="CenterScreen"  WindowState="Maximized"
        Closed="WindowClosed" Loaded="Window_Loaded">

Ive tried setting MaxWidth and MaxHeight on both the host and main window and still no luck.

and in code behind Window:

protected override void OnSourceInitialized(EventArgs e)
    {
        base.OnSourceInitialized(e);
        MinWidth = ActualWidth;
        MinHeight = ActualHeight;
        ClearValue(SizeToContentProperty);
    }

Code on the host control:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="40"/>
        <RowDefinition Height= "*"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition开发者_运维问答 Width="*"/>
    </Grid.ColumnDefinitions>
    <ContentControl x:Name="content"
                    Grid.Row="1" Grid.Column="0" Content="{Binding ContentToDisplay}"
                    VerticalAlignment="Stretch"  VerticalContentAlignment="Stretch"
                    HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch"/>
</Grid>

Ive tried sticking the ContentControl in a ViewBox, DockPanel and still no luck. As soon as I set the content to something "big" (which is within a ScrollViewer) it doesnt bother contain it. What Im trying to achieve is to simply contain the content within the content control within screen.

Thanks!


Maybe as a workaround you could listen to SizeChanged and disable the auto-resizing when you hit the dimensions of the screen?


You have the window marked SizeToContent="WidthAndHeight", so the content is going to size to what it contains. That isn't posted, so it's difficult to say what is causing it. Are you using DataBinding to set any sizes, for example?

Also, you are not setting the maximum width and height of the window to the screen size. That would at least prevent it from getting larger than the screen.

The best thing to do, perhaps, would be to set the window to a specific size, and then in the codebehind, in the Loaded event, set the window to the desired size, or instead set it to maximized once loaded.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜