如何在WPF中拉伸GRID [英] How to stretch the GRID in WPF

查看:476
本文介绍了如何在WPF中拉伸GRID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网格和里面的一些控件我已插入一个链接来查看屏幕



http://i.imgur.com/y2BKKVP.jpg [ ^ ]



I am having a grid and some controls inside it i have inserted a link to view the screen

http://i.imgur.com/y2BKKVP.jpg[^]

		<Page

    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/2006" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"

    xmlns:Custom="http://schemas.microsoft.com/winfx/2006/xaml/composite-font"

    x:Class="FlukeDataDog.Preferences"

     HorizontalAlignment="Stretch" Margin="1,0,0,0" 

    VerticalAlignment="Top" Width="Auto" Height="Auto" x:Name="pgePreferences" KeyDown="pgePreferences_KeyDown" Focusable="False">
    <Page.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="..\Resources\FlukeBasicTheme.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Page.Resources>
        <GroupBox x:Name="grpLanguage" Height="300" Margin="2,0,2,2" Focusable="False" >
        <Grid x:Name="grdMain" Width="Auto" Height="Auto" Margin="0,0,0,0">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="550"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="210"/>
                <RowDefinition Height="10"/>
                <RowDefinition Height="40"/>
            </Grid.RowDefinitions>
			<Button Margin="0,0,0,0" x:Name="btnApply" Content="Apply"  HorizontalAlignment="Center" VerticalAlignment="Center" Width="80" Height="23" Visibility="Hidden" />
        	<Border d:LayoutOverrides="HorizontalAlignment, VerticalAlignment" CornerRadius="15,15,15,15" Focusable="False" x:Name="bdLanguage" removed="#FFA89A6E" BorderBrush="#FF8E8B82" Margin="0,0,-151,0" >
        		<Grid Width="Auto" Height="Auto" VerticalAlignment="Stretch">
        			<Grid.ColumnDefinitions>
        				<ColumnDefinition Width="0.359*"/>
        				<ColumnDefinition Width="0.28*"/>
        				<ColumnDefinition Width="0.361*"/>
        			</Grid.ColumnDefinitions>
        			<Grid.RowDefinitions>
        				<RowDefinition Height="0"/>
        				<RowDefinition Height="11"/>
        				<RowDefinition Height="44"/>
        				<RowDefinition Height="54.5"/>
        				<RowDefinition Height="50"/>
        				<RowDefinition Height="40.5"/>
        				<RowDefinition Height="20"/>
        			</Grid.RowDefinitions>
        			<Label x:Name="lblCurrentLanguage" Height="23" Focusable="False" Content="" Width="Auto" Visibility="Hidden" HorizontalAlignment="Left" VerticalAlignment="Top" Grid.Row="1" Grid.RowSpan="2"/>
        			<Label x:Name="lblCurrentLanguageDisplay" Height="23" Focusable="False" Visibility="Hidden" HorizontalAlignment="Left" Margin="60,0,0,0" VerticalAlignment="Top" Grid.Column="0" Grid.Row="1" Grid.RowSpan="2"/>
        			<Label x:Name="lblSelectLanguage" Content="" Width="Auto" Focusable="False"  Height="23" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="15,0,0,0" Grid.Row="2" />
        			<ComboBox ItemsSource="{Binding}" x:Name="cmbSelectLanguage" IsSynchronizedWithCurrentItem="True" SelectionChanged="cmbSelectLanguage_SelectionChanged" Height="25" HorizontalAlignment="Stretch" MinHeight="0" MinWidth="0" VerticalAlignment="Center" Grid.Column="1" Grid.Row="2" Grid.ColumnSpan="1" Margin="3,0,3,0" Width="175" />
        			<Label x:Name="lblSelectDateFormat" Focusable="False" HorizontalAlignment="Left" Margin="15,0,0,0" VerticalAlignment="Center" Grid.Row="3" Grid.RowSpan="1"/>
        			<ComboBox x:Name="cmbDateFormat" SelectionChanged="cmbDateFormat_SelectionChanged" Height="25" Grid.Column="1" Grid.Row="3" Grid.ColumnSpan="1" VerticalAlignment="Center" Margin="3,0,3,0" Width="175" />
        			<Label x:Name="lblSelectTimeFormat" Focusable="False" Height="23" HorizontalAlignment="Left" VerticalAlignment="Center" Grid.Row="4" Margin="15,0,0,0"/>
        			<ComboBox x:Name="cmbTimeFormat" SelectionChanged="cmbTimeFormat_SelectionChanged" Height="25" Margin="3,7.5,3,12.5" VerticalAlignment="Stretch" Grid.Column="1" Grid.Row="4" Grid.ColumnSpan="1" Width="175" />
                    <ComboBox RenderTransformOrigin="0.5,0.5" x:Name="cmbUnits" VerticalAlignment="Center" Height="25" Grid.Column="1" Grid.Row="5" IsSynchronizedWithCurrentItem="True" SelectionChanged="cmbUnits_SelectionChanged" Grid.ColumnSpan="1" Margin="3,0,3,0" Width="175" /> 
                    <Label HorizontalAlignment="Left" Focusable="False" Margin="15,0,0,0" x:Name="lblSelectUnit" VerticalAlignment="Center" Height="23" VerticalContentAlignment="Center" Content="" Grid.Row="5"/>
        			<ComboBox HorizontalAlignment="Stretch" x:Name="cmbSelectSubUnits" Grid.Row="5" IsSynchronizedWithCurrentItem="True" Grid.Column="2" Grid.RowSpan="1" Grid.ColumnSpan="1" SelectionChanged="cmbSubUnits_SelectionChanged" Margin="9.5,0,68.5,0" Height="25" VerticalAlignment="Center" Width="175" />
        			<Label HorizontalAlignment="Left" x:Name="lblSelectSubUnit" Focusable="False" VerticalAlignment="Center" Content="" Grid.Row="5" VerticalContentAlignment="Center" Margin="15,0,0,0" Grid.RowSpan="1" Visibility="Hidden"/>
                     <ComboBox RenderTransformOrigin="0.5,0.5" x:Name="cmbOVUnits" VerticalAlignment="Center" Height="25" Grid.Column="1" Grid.Row="6" IsSynchronizedWithCurrentItem="True"  Grid.ColumnSpan="1" Margin="3,0,3,0" Width="175" /> 
                    <Label x:Name="lblOVUnit" Content="Select OV Unit" Width="Auto" Focusable="False" Height="23" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="15,0,0,0" Grid.Row="7" />
        	                    
                </Grid>
        	</Border>
        	
            <Border d:LayoutOverrides="HorizontalAlignment, VerticalAlignment" CornerRadius="15,15,15,15" Focusable="False" x:Name="bdAutoStart" removed="#FFA89A6E" BorderBrush="#FF8E8B82" Margin="0,0,-151,0" Grid.Row="3" >
                <CheckBox HorizontalAlignment="Left" Focusable="False" Margin="15,0,0,0" VerticalAlignment="Center" Height="23" VerticalContentAlignment="Center" Content="" x:Name="chkAutoShow"   Checked="chkAutoDisplay_Checked" Unchecked="chkAutoDisplay_Unchecked" />
            </Border>
        </Grid>
    </GroupBox>  
     
</Page>





But when i add a new label it is not stretching i am unable to see the label and combobox fully.



But when i add a new label it is not stretching i am unable to see the label and combobox fully.

推荐答案

Please see my comment to the question.



This is the key:

http://msdn.microsoft.com/en-us/library/system.windows.window.sizetocontent(v=vs.110).aspx[^],

http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.horizontalalignment(v=vs.110).aspx[^],

http ://msdn.microsoft.com/en-us/library/system.windows.controls.control.horizontalcontentalignment(v=vs.110).aspx[^].



You need to understand this:

http://msdn.microsoft.com/en-us/library/ms745058%28v=vs.110%29.aspx[^],

http://msdn.microsoft.com/en-us/library/ms748828%28v=vs.110%29.aspx[^].



I will be able to answer in a way more specific to your case if you provide more adequate code sample which would allow to see the problem immediately through Visual Studio. One (highly simplified, please!) complete self-contained XAML sample will do.



—SA
Please see my comment to the question.

This is the key:
http://msdn.microsoft.com/en-us/library/system.windows.window.sizetocontent(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.horizontalalignment(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.controls.control.horizontalcontentalignment(v=vs.110).aspx[^].

You need to understand this:
http://msdn.microsoft.com/en-us/library/ms745058%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/ms748828%28v=vs.110%29.aspx[^].

I will be able to answer in a way more specific to your case if you provide more adequate code sample which would allow to see the problem immediately through Visual Studio. One (highly simplified, please!) complete self-contained XAML sample will do.

—SA


I might be wrong, but there are some glaring things in your XAML that jump out as being highly contradictory with what you are trying to achieve.



Let’s start with this:

I might be wrong, but there are some glaring things in your XAML that jump out as being highly contradictory with what you are trying to achieve.

Let's start with this:
<ColumnDefinition Width="0.28*"/>



Now, I might be incorrect but my understanding is that * represents an equal portion of the available width. So in this case you are telling your column to be 28% of 1/3rd (you declare 3 columns) of the width of your grid.



Now, when it comes to your combo box (I’ve stripped out some of your attributes to simplify what I’m explaining.


Now, I might be incorrect but my understanding is that * represents an equal portion of the available width. So in this case you are telling your column to be 28% of 1/3rd (you declare 3 columns) of the width of your grid.

Now, when it comes to your combo box (I've stripped out some of your attributes to simplify what I'm explaining.

<ComboBox Width="175" />



You are declaring it with a fixed width of 175.



Now let me ask you this... what happens if your grid column’s size is only 150 and you have fixed the width of your combobox to take up all of that space? Well, the combo box will get cut off.



Now, looking at your labels.

I think all of them need to have \"Grid.ColumnSpan=3\" added to them as at the moment you are limiting their space to just the first column, yet they could have long text and as a result will be cut off by the limited size.


You are declaring it with a fixed width of 175.

Now let me ask you this... what happens if your grid column's size is only 150 and you have fixed the width of your combobox to take up all of that space? Well, the combo box will get cut off.

Now, looking at your labels.
I think all of them need to have "Grid.ColumnSpan=3" added to them as at the moment you are limiting their space to just the first column, yet they could have long text and as a result will be cut off by the limited size.


Oh, in addition to my other solution.



You have now added a picture which make s another issue plain as day.





Oh, in addition to my other solution.

You have now added a picture which makes another issue plain as day.


<Grid.RowDefinitions>
<RowDefinition Height="0"/>
<RowDefinition Height="11"/>
<RowDefinition Height="44"/>
<RowDefinition Height="54.5"/>
<RowDefinition Height="50"/>
<RowDefinition Height="40.5"/>
<RowDefinition Height="20"/>
</Grid.RowDefinitions>





The sum of their total height is 220.

This I believe is the row the above grid sits in.



The sum of their total height is 220.
This I believe is the row the above grid sits in.

<RowDefinition Height="210"/>





It has a height of 210. Try increasing this to 220 or I would probably go for 230 to see how it looks.



That might help your issue.



It has a height of 210. Try increasing this to 220 or I would probably go for 230 to see how it looks.

That might help your issue.


这篇关于如何在WPF中拉伸GRID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆