简体   繁体   English

无法获取TextBoxes(2)填充网格空间

[英]Cannot get TextBoxes(2) to fill grid space

So I'm convinced that this is probably an easy answer and I'm just not seeing it, but after 20 google searches and 3 days of trying to figure it out on my own, I surrender. 因此,我坚信这可能是一个简单的答案,但我只是没有看到它,但是经过20次Google搜索和3天的尝试,我自己投降了。 The xaml I'm using is below. 我正在使用的xaml在下面。 What I need is for the two textboxes to fill all remaining space and to be equally sized. 我需要的是两个文本框填充所有剩余空间并保持大小相等。 I've tried StackPanel, DockPanel, Grid (as it is below), and even nesting each of these and nesting a UniformGrid. 我已经尝试过StackPanel,DockPanel,Grid(如下所示),甚至嵌套了其中的每一个并嵌套了UniformGrid。 Nothing seems to work, I just get 2 textboxes with whatever height I specify for MinHeight. 似乎没有任何效果,我只得到2个文本框,它们具有为MinHeight指定的高度。 Changing the Grid.Row setting from Auto to * just results in the TextBoxes centering themselves and moving as the window shrinks and grows. Grid.Row设置从“自动”更改为*只会导致文本框居中并随着窗口的缩小和增长而移动。

        <TabItem Header="Notes" Name="notesTab">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="Auto"/>
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="23"/>
                    <RowDefinition Height="Auto"/>
                </Grid.RowDefinitions>
                <Label Content="Select Contact:" Height="28" Grid.Row="0" Grid.Column="0" HorizontalAlignment="Right" FontStyle="Italic" FontWeight="Bold"></Label>
                <ComboBox Grid.Column="1" Grid.Row="0" Height="23" HorizontalAlignment="Right" Width="150" ItemsSource="{Binding Source={StaticResource contactList}}" DataContext="{Binding Source={StaticResource contactList}}" Name="contactSelectionBox"></ComboBox>
                <WrapPanel Grid.Row="1" Height="Auto" Grid.ColumnSpan="2" Margin="5">
                    <WrapPanel.Width>
                        <Binding ElementName="callNotes" Path="ActualWidth" />
                    </WrapPanel.Width>
                    <Label Content="TAD" Width="Auto" Name="tadShortcut" MouseLeftButtonDown="tadShortcut_MouseLeftButtonDown" MouseEnter="notesLabel_MouseEnter" MouseLeave="notesLabel_MouseLeave" />
                    <Label Content="VI" Width="Auto" Name="verifyInfoShortcut" MouseLeftButtonDown="verifyInfoShortcut_MouseLeftButtonDown" MouseEnter="notesLabel_MouseEnter" MouseLeave="notesLabel_MouseLeave" />
                    <Label Content="Not there" Width="Auto" Name="uipShortcut" MouseLeftButtonDown="uipShortcut_MouseLeftButtonDown" MouseEnter="notesLabel_MouseEnter" MouseLeave="notesLabel_MouseLeave" />
                    <Label Content="WUSP" Width="Auto" Name="wuspShortcut" MouseLeftButtonDown="wuspShortcut_MouseLeftButtonDown" MouseEnter="notesLabel_MouseEnter" MouseLeave="notesLabel_MouseLeave" />
                    <Label Content="FNYD" Width="Auto" Name="fnydShortcut" MouseLeftButtonDown="fnydShortcut_MouseLeftButtonDown" MouseEnter="notesLabel_MouseEnter" MouseLeave="notesLabel_MouseLeave" />
                    <Label Content="Not Employed" Width="Auto" Name="notEmployedShortcut" MouseLeftButtonDown="notEmployedShortcut_MouseLeftButtonDown" MouseEnter="notesLabel_MouseEnter" MouseLeave="notesLabel_MouseLeave" />
                    <Label Content="Message" Width="Auto" Name="messageShortcut" MouseLeftButtonDown="messageShortcut_MouseLeftButtonDown" MouseEnter="notesLabel_MouseEnter" MouseLeave="notesLabel_MouseLeave" />
                    <Label Content="Late Fees" Width="Auto" Name="lateFeesShortcut" MouseLeftButtonDown="lateFeesShortcut_MouseLeftButtonDown" MouseEnter="notesLabel_MouseEnter" MouseLeave="notesLabel_MouseLeave" />
                    <Label Content="Payoff" Width="Auto" Name="payoffReqShortcut" MouseLeftButtonDown="payoffReqShortcut_MouseLeftButtonDown" MouseEnter="notesLabel_MouseEnter" MouseLeave="notesLabel_MouseLeave" />
                    <Label Content="No Commit" Width="Auto" Name="noCommit" MouseLeftButtonDown="noCommit_MouseLeftButtonDown" MouseEnter="notesLabel_MouseEnter" MouseLeave="notesLabel_MouseLeave" />
                    <Label Content="12/12" Width="Auto" Name="twelve12" MouseLeftButtonDown="twelve12_MouseLeftButtonDown" MouseEnter="notesLabel_MouseEnter" MouseLeave="notesLabel_MouseLeave" />
                    <Label Content="No ID" Width="Auto" Name="vmNoID" MouseLeftButtonDown="vmNoID_MouseLeftButtonDown" MouseEnter="notesLabel_MouseEnter" MouseLeave="notesLabel_MouseLeave" />
                    <Label Content="Wrong #" Width="Auto" Name="wrongNumber" MouseLeftButtonDown="wrongNumber_MouseLeftButtonDown" MouseEnter="notesLabel_MouseEnter" MouseLeave="notesLabel_MouseLeave" />
                    <Label Content="Day Off" Width="Auto" Name="dayOff" MouseLeftButtonDown="dayOff_MouseLeftButtonDown" MouseEnter="notesLabel_MouseEnter" MouseLeave="notesLabel_MouseLeave" />
                    <Label Content="FPD" Width="Auto" Name="firstPaymentDefault" MouseLeftButtonDown="firstPaymentDefault_MouseLeftButtonDown" MouseEnter="notesLabel_MouseEnter" MouseLeave="notesLabel_MouseLeave" />
                    <Label Content="Repo Pmnt" Width="Auto" Name="repoPayment" MouseLeftButtonDown="repoPayment_MouseLeftButtonDown" MouseEnter="notesLabel_MouseEnter" MouseLeave="notesLabel_MouseLeave" />
                    <Label Content="CS Xfer" Width="Auto" Name="custServ" MouseLeftButtonDown="custServ_MouseLeftButtonDown" MouseEnter="notesLabel_MouseEnter" MouseLeave="notesLabel_MouseLeave" />
                    <Label Content="Spanish" Width="Auto" Name="spanish" MouseLeftButtonDown="spanish_MouseLeftButtonDown" MouseEnter="notesLabel_MouseEnter" MouseLeave="notesLabel_MouseLeave" />
                </WrapPanel>
                <MyNamespace:WatermarkTextBox Grid.Row="2" Grid.ColumnSpan="2" Style="{StaticResource TextBox Style}" MinHeight="98" Margin="15,5,15,5" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" AcceptsReturn="True" TextWrapping="Wrap" VerticalScrollBarVisibility="Auto" Name="callTwoNotes"  TextChanged="callTwoNotes_TextChanged" FontWeight="Bold" IsEnabled="False" ClipToBounds="False"></MyNamespace:WatermarkTextBox>
                <Button Grid.Row="3" Grid.ColumnSpan="2" Height="23" Name="flipFlopButton" VerticalAlignment="Center" Width="75" Click="flipFlopButton_Click">
                        <Image Source="Images/FlipFlop.gif" />
                </Button>
                <MyNamespace:WatermarkTextBox Grid.Row="4" Grid.ColumnSpan="2" Style="{StaticResource TextBox Style}" MinHeight="98" Margin="15,5,15,5" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" AcceptsReturn="True" TextWrapping="Wrap" VerticalScrollBarVisibility="Auto" Name="callNotes"  TextChanged="callNotes_TextChanged" FontWeight="Bold"></MyNamespace:WatermarkTextBox>
            </Grid>
    </TabItem>

The xaml for the style on the TextBoxes is below, just for completeness. 出于完整性考虑,下面是TextBoxes上样式的xaml。

<Style x:Key="TextBox Style" TargetType="{x:Type MyNamespace:WatermarkTextBox}">
        <Setter Property="CharacterCasing" Value="Upper" />
        <Setter Property="HorizontalAlignment" Value="Left" />
        <Setter Property="VerticalAlignment" Value="Top" />
        <Setter Property="Height" Value="23" />
        <Setter Property="Width" Value="Auto" />
        <Setter Property="SelectAllOnGotFocus" Value="True" />
    </Style>

There are two issues with the code posted 发布的代码有两个问题

The Grid's RowDefintion for the TextBoxes is listed as Auto instead of * . RowDefintion的网格的RowDefintion列为Auto而不是* This means the Row will be sized based on the size of the cell contents, not based on the Grid's size. 这意味着将根据单元格内容的大小而不是网格的大小来确定行的大小。 Change this to * so it's size will be based on available space. 将此更改为*以便其大小取决于可用空间。

<Grid.RowDefinitions>
    <RowDefinition Height="Auto"/>
    <RowDefinition Height="Auto"/>
    <RowDefinition Height="*"/>
    <RowDefinition Height="23"/>
    <RowDefinition Height="*"/>
</Grid.RowDefinitions>

The other problem is your TextBox style is defining a Height for your item. 另一个问题是您的TextBox样式正在为项目定义Height This means it will keep the TextBox at the specified Height and not re-size it to fit the available space. 这意味着它将文本框保持在指定的高度,而不调整其大小以适合可用空间。 You can either remove this style setter, or set the Height on your TextBoxes in the Grid to Auto. 您可以删除此样式设置器,也可以将“网格”中“文本框”上的“高度”设置为“自动”。

Remove this setter 删除此二传手

<Style x:Key="TextBox Style" TargetType="{x:Type MyNamespace:WatermarkTextBox}">
    <Setter Property="Height" Value="23" />
    ...
</Style>

Or set the Height property on your TextBox tag to Auto. 或将TextBox标记上的Height属性设置为Auto。 Setting the property in the object's tag will overwrite a styled property 在对象的标签中设置属性会覆盖样式化的属性

<MyNamespace:WatermarkTextBox Height="Auto" Grid.Row="2" ... />
<MyNamespace:WatermarkTextBox Height="Auto" Grid.Row="4" ... />

This was answered a few minutes ago by someone else but now the answer it gone. 几分钟前有人回答了这个问题,但现在答案消失了。 Try deleting these lines: 尝试删除这些行:

    <Setter Property="HorizontalAlignment" Value="Left" /> 
    <Setter Property="VerticalAlignment" Value="Top" />

As might be overwriting the Stretch 可能会覆盖拉伸

Edit: Ok i tried your code and what you need to change is your Grid RowDefinitions, try 编辑:好的,我尝试了您的代码,您需要更改的是您的Grid RowDefinitions,尝试

<Grid.RowDefinitions>
    <RowDefinition Height="Auto"/>
    <RowDefinition Height="Auto"/>
    <RowDefinition Height="2*"/>
    <RowDefinition Height="23"/>
    <RowDefinition Height="2*"/>
</Grid.RowDefinitions>

and you might want to remove your minheight aswell. 并且您可能还希望删除自己的身高。 In fact, you should try to remove all you explicit heights and widths, let WPF take care of that, thats what the layout system is for. 实际上,您应该尝试删除所有显式的高度和宽度,让WPF负责,这就是布局系统的作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM