【问题标题】:WPF Grid content not spanning the full widthWPF 网格内容不跨越整个宽度
【发布时间】:2014-05-12 14:43:51
【问题描述】:

摘要

为了让未来的读者更清楚,这里是问题及其解决方法的摘要。

我的网格位于一个页面中,该页面用作 tabitem 的内容。在运行时,网格拒绝填充整个 tabitem 区域,如下面的屏幕截图所示。

问题在于 tabitem 的样式包含一个内容模板,该模板通过 contentpresenter 显示页面,内部是一个水平堆栈面板。堆栈面板覆盖了页面中网格的拉伸属性。

解决方案是用网格替换样式中的堆栈面板。没有更多的布局问题。

原始问题

我有一个 StackPanel,在网格内有一个标签和文本框,我想将文本框的宽度绑定到网格中前两列的宽度。我通常使用将文本框宽度绑定到其父级的方法,并尝试将 that 值绑定到具有正确值的 something,但我尝试直接绑定也是如此,没有运气。

这是我的 xaml:

<Page x:Class="BPC.CPI.Pages.CustomerMaintenance"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  xmlns:cpihelpers="clr-namespace:BPC.CPI.ControlHelpers"
  xmlns:robertData="clr-namespace:BPC.Robert.DataEncapsulation;assembly=BPC.Robert"
  xmlns:robertGlobals="clr-namespace:BPC.Robert.Globals;assembly=BPC.Robert"
  xmlns:avalon="clr-namespace:AvalonLambdas;assembly=AvalonLambdas"
  xmlns:converters="clr-namespace:BPC.Utilities.ValueConverters;assembly=BPCUtilities"
  mc:Ignorable="d" 
  d:DesignHeight="602" d:DesignWidth="1149"
  Title="CustomerMaintenance" Name="ThisPage"
  DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Page.Resources>
    <converters:IntegerToBooleanConverter x:Key="IntegerToBooleanConverter"></converters:IntegerToBooleanConverter>
</Page.Resources>

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"></RowDefinition>
        <RowDefinition Height="*"></RowDefinition>
    </Grid.RowDefinitions>
    <Grid Grid.Row="0">
        <ComboBox ItemsSource="{Binding PageModel.PageUser.AssociatedCustomers}" Name="CbCustomerName" 
                  SelectedItem="{Binding PageModel.PageCustomer}"
                  Style="{StaticResource HeadingTwoComboBox}" cpihelpers:ComboBoxHelper.MaxDropDownItems="10">
            <ComboBox.ItemContainerStyle>
                <Style TargetType="ComboBoxItem">
                    <Setter Property="LayoutTransform" Value="{StaticResource Transform.5x}"></Setter>
                    <Setter Property="Foreground" Value="Black"></Setter>
                </Style>
            </ComboBox.ItemContainerStyle>
        </ComboBox>
    </Grid>
    <Grid Name="GridMainContent" Grid.Row="1">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"></ColumnDefinition>
            <ColumnDefinition Width="20"></ColumnDefinition>
            <ColumnDefinition Width="*"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <Grid Name="GridGeneralCustomerInfo" Grid.Column="0" >
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="5"></RowDefinition>
                <RowDefinition Height=".35*"></RowDefinition>
                <RowDefinition Height="1*"></RowDefinition>
                <RowDefinition Height=".8*"></RowDefinition>
            </Grid.RowDefinitions>
            <StackPanel Name="SpCustomerNotes" Orientation="Vertical" Grid.Column="0" Grid.Row="1"
                        HorizontalAlignment="Left">
                <Label Name="LblCustomerNotes" Target="{Binding ElementName=TxtCustomerNotes}" Style="{StaticResource PromptText}" Content="{Binding Path=Text, ElementName=CbCustomerName}" ContentStringFormat="What is special about {0}?"></Label>
                <TextBox Name="TxtCustomerNotes" AcceptsReturn="True" TextWrapping="Wrap" ScrollViewer.VerticalScrollBarVisibility="Auto"
                         MaxHeight="{Binding Path=ActualHeight, ElementName=SpCustomerNotes, Converter={avalon:LambdaValueConverter (param * 0.65)}}"
                         Text="{Binding PageModel.PageCustomer.CustomerInfo.CP_NOTES}"></TextBox>
            </StackPanel>
            <Grid Grid.Column="0" Grid.Row="2">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"></RowDefinition>
                    <RowDefinition Height="*"></RowDefinition>
                    <RowDefinition Height="*"></RowDefinition>
                    <RowDefinition Height="*"></RowDefinition>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"></ColumnDefinition>
                    <ColumnDefinition Width="*"></ColumnDefinition>
                </Grid.ColumnDefinitions>
                <TextBlock Style="{StaticResource HeadingFourBrown}" Grid.Column="0" Grid.ColumnSpan="2" Text="{Binding Path=Text, ElementName=CbCustomerName, StringFormat=A few questions about \{0\}...}"></TextBlock>
                <StackPanel Name="SpReportingType" Orientation="Vertical" Grid.Row="1" Grid.Column="0" Margin="6,0,0,0">
                    <Label Name="LblReportingType" Target="{Binding ElementName=TxtReportingType}" Style="{StaticResource PromptText}" Content="Contractual or actual reporting?"></Label>
                    <ComboBox Name="CbReportingType" Style="{StaticResource ComboBoxWithoutBackground}" SelectedValuePath="REPORTING_TYPE_ID"
                              ItemsSource="{Binding PageModel.PageSelection.ReportingTypes}" DisplayMemberPath="NAME"
                              SelectedValue="{Binding PageModel.PageCustomer.CustomerInfo.REPORTING_TYPE_ID}"></ComboBox>
                </StackPanel>
                <StackPanel Name="SpReportingLevel" Orientation="Vertical" Grid.Row="2" Grid.Column="0" Margin="6,0,0,0">
                    <Label Name="LblReportingLevel" Target="{Binding ElementName=TxtReportingLevel}" Style="{StaticResource PromptText}" Content="How should we roll up data?"></Label>
                    <ComboBox Name="CbReportingLevel" Style="{StaticResource ComboBoxWithoutBackground}" SelectedValuePath="REPORTING_LEVEL_ID"
                              ItemsSource="{Binding PageModel.PageSelection.ReportingLevel}" DisplayMemberPath="NAME"
                              SelectedValue="{Binding PageModel.PageCustomer.CustomerInfo.REPORTING_LEVEL_ID}"></ComboBox>
                </StackPanel>
                <StackPanel Name="SpReportingPounds" Orientation="Vertical" Grid.Row="3" Grid.Column="0" Margin="6,0,0,0">
                    <Label Name="LblReportingPounds" Target="{Binding ElementName=TxtReportingPounds}" Style="{StaticResource PromptText}" Content="Net or gross weights?"></Label>
                    <ComboBox Name="CbReportingPounds" Style="{StaticResource ComboBoxWithoutBackground}" SelectedValuePath="REPORTING_LBS_ID"
                              ItemsSource="{Binding PageModel.PageSelection.ReportingPounds}" DisplayMemberPath="NAME"
                              SelectedValue="{Binding PageModel.PageCustomer.CustomerInfo.REPORTING_LBS_ID}"></ComboBox>
                </StackPanel>
                <StackPanel Name="SpInventoryLevel" Orientation="Vertical" Grid.Row="1" Grid.Column="1" Margin="6,0,0,0">
                    <Label Name="LblInventoryLevel" Target="{Binding ElementName=TxtInventoryLevel}" Style="{StaticResource PromptText}" Content="Customer or title?"></Label>
                    <ComboBox Name="CbInventoryLevel" Style="{StaticResource ComboBoxWithoutBackground}" SelectedValuePath="INV_RPT_LEVEL_ID"
                              ItemsSource="{Binding PageModel.PageSelection.InventoryReportingLevel}" DisplayMemberPath="NAME"
                              SelectedValue="{Binding PageModel.PageCustomer.CustomerInfo.INV_RPT_LEVEL_ID}"></ComboBox>
                </StackPanel>
                <StackPanel Name="SpReportByPO" Orientation="Vertical" Grid.Row="2" Grid.Column="1" Margin="6,0,0,0">
                    <Label Name="LblReportByPO" Target="{Binding ElementName=TxtReportByPO}" Style="{StaticResource PromptText}" Content="Do we report by PO?"></Label>
                    <CheckBox Name="ChkReportByPO" Style="{DynamicResource SliderCheckBox}" HorizontalAlignment="Left" Checked="ChkReportByPO_Checked"
                              IsChecked="{Binding PageModel.PageCustomer.CustomerInfo.IS_REPORTED_BY_PO, Converter={StaticResource IntegerToBooleanConverter}}"></CheckBox>
                </StackPanel>
            </Grid>
            <Grid Name="GridMiscellaneous" Grid.Column="0" Grid.Row="3">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"></RowDefinition>
                    <RowDefinition Height="*"></RowDefinition>
                    <RowDefinition Height="5"></RowDefinition>
                    <RowDefinition Height="*"></RowDefinition>
                    <RowDefinition Height="5"></RowDefinition>
                    <RowDefinition Height="*"></RowDefinition>
                </Grid.RowDefinitions>
                <TextBlock Style="{StaticResource HeadingFourBrown}" Grid.Row="0" Text="Other stuff"></TextBlock>
                <StackPanel Name="SpSaveDirectory" Orientation="Vertical" Grid.Row="1" Margin="6,0,0,0">
                    <Label Name="LblSaveDirectory" Target="{Binding ElementName=TxtSaveDirectory}" Style="{StaticResource PromptText}" Content="Where should saved reports go?"></Label>
                    <TextBox Name="TxtSaveDirectory" ></TextBox>
                </StackPanel>
                <StackPanel Name="SpFrequency" Orientation="Vertical" Grid.Row="3" Margin="6,0,0,0">
                    <Label Name="LblFrequency" Target="{Binding ElementName=TxtFrequency}" Style="{StaticResource PromptText}" Content="{Binding Path=Text, ElementName=CbCustomerName}" ContentStringFormat="How often do you run reports for {0}?"></Label>
                    <TextBox Name="TxtFrequency" ></TextBox>
                </StackPanel>
                <StackPanel Name="SpHoursPerReport" Orientation="Vertical" Grid.Row="5" Margin="6,0,0,0">
                    <Label Name="LblHoursPerReport" Target="{Binding ElementName=TxtHoursPerReport}" Style="{StaticResource PromptText}" Content="How many hours do you spend per report (whole hours)?"></Label>
                    <TextBox Name="TxtHoursPerReport" ></TextBox>
                </StackPanel>
            </Grid>
        </Grid>
        <Grid Name="GridCustomerPeopleAndTitles" Grid.Column="2">
            <Grid.RowDefinitions>
                <RowDefinition Height="1.2*"></RowDefinition>
                <RowDefinition Height="1.5*"></RowDefinition>
                <RowDefinition Height=".8*"></RowDefinition>
            </Grid.RowDefinitions>
            <Grid Name="GridCustomerPeople" Grid.Row="0">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"></RowDefinition>
                    <RowDefinition Height="*"></RowDefinition>
                    <RowDefinition Height="*"></RowDefinition>
                    <RowDefinition Height="*"></RowDefinition>
                </Grid.RowDefinitions>
                <TextBlock Name="TxtbCustomerPeople" Grid.Row="0" Style="{StaticResource HeadingFourBrown}" Text="{Binding Path=Text, ElementName=CbCustomerName, StringFormat=These people also work with \{0\}}"></TextBlock>
                <StackPanel Orientation="Vertical" Grid.Row="1" Margin="6,0,0,0">
                    <Label Name="LblSalesReps" Style="{StaticResource PromptText}" Content="Sales Reps:"></Label>
                    <TextBlock Name="TxtbSalesReps" Text="{Binding PageModel.PageCustomer.SalesReps}" TextTrimming="CharacterEllipsis" MaxWidth="550"></TextBlock>
                </StackPanel>
                <StackPanel Orientation="Vertical" Grid.Row="2" Margin="6,0,0,0">
                    <Label Name="LblBillers" Style="{StaticResource PromptText}" Content="Billers:"></Label>
                    <TextBlock Name="TxtbBillers" Text="{Binding PageModel.PageCustomer.Billers}" TextTrimming="CharacterEllipsis" MaxWidth="550"></TextBlock>
                </StackPanel>
                <StackPanel Orientation="Vertical" Grid.Row="3" Margin="6,0,0,0" HorizontalAlignment="Stretch">
                    <Label Name="LblCams" Style="{StaticResource PromptText}" Content="Customer Account Managers:"></Label>
                    <TextBlock Name="TxtbCams" Text="{Binding PageModel.PageCustomer.CAMs}" TextTrimming="CharacterEllipsis"></TextBlock>
                </StackPanel>
            </Grid>
            <Grid Name="GridTitleGroups" Grid.Row="1">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"></RowDefinition>
                    <RowDefinition Height="*"></RowDefinition>
                </Grid.RowDefinitions>
                <TextBlock Name="TxtbTitleGroups" Grid.Row="0" Style="{StaticResource HeadingFourBrown}" Text="{Binding Path=Text, ElementName=CbCustomerName, StringFormat=Group \{0\}\'s titles}"></TextBlock>
                <Grid Grid.Row="1">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="15"></ColumnDefinition>
                        <ColumnDefinition Width="*"></ColumnDefinition>
                        <ColumnDefinition Width="10"></ColumnDefinition>
                        <ColumnDefinition Width="*"></ColumnDefinition>
                        <ColumnDefinition Width="15"></ColumnDefinition>
                    </Grid.ColumnDefinitions>
                    <ListBox Name="ListTitles" ItemsSource="{Binding}" Grid.Column="1" ToolTip="{Binding Source={x:Static robertGlobals:Messages.DragTitles}}">

                    </ListBox>
                    <Border Grid.Column="3" Style="{StaticResource BlueBorder}">
                        <Grid Name="GridTitleGroupItems" Grid.Column="3">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="30"></RowDefinition>
                            <RowDefinition Height="*"></RowDefinition>
                        </Grid.RowDefinitions>
                            <Rectangle Grid.Row="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Fill="{StaticResource BrownAccentBrush}" OpacityMask="{StaticResource StrongOpacityMaskBrush}"
                                       ToolTip="{Binding Source={x:Static robertGlobals:Messages.DragTitlesNew}}"></Rectangle>
                            <TextBlock Name="TxtbNewTitleGroup" Grid.Row="0" Text="Create a New Group" AllowDrop="True" HorizontalAlignment="Center"
                                 VerticalAlignment="Center" Style="{StaticResource GreenAccentText}" ToolTip="{Binding Source={x:Static robertGlobals:Messages.DragTitlesNew}}"></TextBlock>
                            <TreeView Name="TreeTitleGroups" ItemsSource="{Binding}" AllowDrop="True" Grid.Row="1" BorderBrush="{x:Null}" ToolTip="{Binding Source={x:Static robertGlobals:Messages.DragTitles}}">

                        </TreeView>
                    </Grid>
                    </Border>
                </Grid>
            </Grid>
            <Grid Name="GridCustomerGroups" Grid.Row="2">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"></RowDefinition>
                    <RowDefinition Height="*"></RowDefinition>
                </Grid.RowDefinitions>
                <TextBlock Name="TxtbCustomerGroups" Grid.Row="0" Style="{StaticResource HeadingFourBrown}" Text="{Binding Path=Text, ElementName=CbCustomerName, StringFormat=Group \{0\}\'s customers}"></TextBlock>
            </Grid>
        </Grid>
    </Grid>
</Grid>
</Page>

我已经从各种问题和其他网站尝试了几种推荐的解决方法,但没有任何效果。我尝试在要绑定的单元格上粘贴边框并绑定到其尺寸,使用标签,嵌套另一个网格,x:命名 ColumnDefinition 并绑定到 ActualWidth 等等。没有。作品。

这似乎在 wpf 中应该非常简单,而我只是缺少关键的、次要的部分。我在这件事上大概浪费了三个小时。我做错了什么?

编辑:

我刚刚意识到我似乎绑定到整个 GridMainContent 的宽度 - 我没有。这只是一种虚拟值,已被我尝试过的每种不同的解决方案所取代。

这是我的问题的图片。内容应填满整个选项卡。请注意,周围网格的大小 down 以适合内容,而不是内容大小 up 以适合网格。我已经为我的内容定义了在每个父元素上设置 Horizo​​ntalAlignment=Stretch 的隐式样式,但似乎没有任何效果。

我期望它做的是让左侧部分(带有下拉菜单)和右侧部分(带有销售代表等)的宽度相等,填充选项卡的整个内容区域。

【问题讨论】:

  • 尽量避免绑定,使用 WPF 布局。
  • 这是不可接受的。我需要宽度与其父宽度正确缩放。
  • 给你的网格孩子 Grid.ColumnSpan = 4 有什么问题?
  • 我需要其他元素来占据剩余的列。也许我误解了你的问题。
  • 你能用实际的绑定源更新你的 XAML 吗?

标签: wpf xaml binding


【解决方案1】:

尝试将下面的 xaml 粘贴为 WPF 窗口内容,不要使用制表符 - 我已经这样做了,布局很好:

您能否确认布局是否正确?我最好的选择是 - 根网格的父级以某种方式限制了可用空间,因此可用宽度小于屏幕宽度。检查所有可用空间是否都用石灰粉刷。

XAML:

<Grid Background="Lime">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>
        <Grid Grid.Row="0">
            <ComboBox ItemsSource="{Binding PageModel.PageUser.AssociatedCustomers}" Name="CbCustomerName" 
                  SelectedItem="{Binding PageModel.PageCustomer}"
                  >
                <ComboBox.ItemContainerStyle>
                    <Style TargetType="ComboBoxItem">
                        <Setter Property="LayoutTransform" ></Setter>
                        <Setter Property="Foreground" Value="Black"></Setter>
                    </Style>
                </ComboBox.ItemContainerStyle>
            </ComboBox>
        </Grid>
        <Grid Name="GridMainContent" Grid.Row="1">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"></ColumnDefinition>
                <ColumnDefinition Width="20"></ColumnDefinition>
                <ColumnDefinition Width="*"></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <Grid Name="GridGeneralCustomerInfo" Grid.Column="0" >
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"></ColumnDefinition>
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="5"></RowDefinition>
                    <RowDefinition Height=".35*"></RowDefinition>
                    <RowDefinition Height="1*"></RowDefinition>
                    <RowDefinition Height=".8*"></RowDefinition>
                </Grid.RowDefinitions>
                <StackPanel Name="SpCustomerNotes" Orientation="Vertical" Grid.Column="0" Grid.Row="1"
                        HorizontalAlignment="Left">
                    <Label Name="LblCustomerNotes" Target="{Binding ElementName=TxtCustomerNotes}" Content="{Binding Path=Text, ElementName=CbCustomerName}" ContentStringFormat="What is special about {0}?"></Label>
                    <TextBox Name="TxtCustomerNotes" AcceptsReturn="True" TextWrapping="Wrap" ScrollViewer.VerticalScrollBarVisibility="Auto"
                         Text="{Binding PageModel.PageCustomer.CustomerInfo.CP_NOTES}"></TextBox>
                </StackPanel>
                <Grid Grid.Column="0" Grid.Row="2">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"></RowDefinition>
                        <RowDefinition Height="*"></RowDefinition>
                        <RowDefinition Height="*"></RowDefinition>
                        <RowDefinition Height="*"></RowDefinition>
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"></ColumnDefinition>
                        <ColumnDefinition Width="*"></ColumnDefinition>
                    </Grid.ColumnDefinitions>
                    <TextBlock Grid.Column="0" Grid.ColumnSpan="2" Text="{Binding Path=Text, ElementName=CbCustomerName, StringFormat=A few questions about \{0\}...}"></TextBlock>
                    <StackPanel Name="SpReportingType" Orientation="Vertical" Grid.Row="1" Grid.Column="0" Margin="6,0,0,0">
                        <Label Name="LblReportingType" Target="{Binding ElementName=TxtReportingType}" Content="Contractual or actual reporting?"></Label>
                        <ComboBox Name="CbReportingType" SelectedValuePath="REPORTING_TYPE_ID"
                              ItemsSource="{Binding PageModel.PageSelection.ReportingTypes}" DisplayMemberPath="NAME"
                              SelectedValue="{Binding PageModel.PageCustomer.CustomerInfo.REPORTING_TYPE_ID}"></ComboBox>
                    </StackPanel>
                    <StackPanel Name="SpReportingLevel" Orientation="Vertical" Grid.Row="2" Grid.Column="0" Margin="6,0,0,0">
                        <Label Name="LblReportingLevel" Target="{Binding ElementName=TxtReportingLevel}" Content="How should we roll up data?"></Label>
                        <ComboBox Name="CbReportingLevel" SelectedValuePath="REPORTING_LEVEL_ID"
                              ItemsSource="{Binding PageModel.PageSelection.ReportingLevel}" DisplayMemberPath="NAME"
                              SelectedValue="{Binding PageModel.PageCustomer.CustomerInfo.REPORTING_LEVEL_ID}"></ComboBox>
                    </StackPanel>
                    <StackPanel Name="SpReportingPounds" Orientation="Vertical" Grid.Row="3" Grid.Column="0" Margin="6,0,0,0">
                        <Label Name="LblReportingPounds" Target="{Binding ElementName=TxtReportingPounds}" Content="Net or gross weights?"></Label>
                        <ComboBox Name="CbReportingPounds" SelectedValuePath="REPORTING_LBS_ID"
                              ItemsSource="{Binding PageModel.PageSelection.ReportingPounds}" DisplayMemberPath="NAME"
                              SelectedValue="{Binding PageModel.PageCustomer.CustomerInfo.REPORTING_LBS_ID}"></ComboBox>
                    </StackPanel>
                    <StackPanel Name="SpInventoryLevel" Orientation="Vertical" Grid.Row="1" Grid.Column="1" Margin="6,0,0,0">
                        <Label Name="LblInventoryLevel" Target="{Binding ElementName=TxtInventoryLevel}" Content="Customer or title?"></Label>
                        <ComboBox Name="CbInventoryLevel" SelectedValuePath="INV_RPT_LEVEL_ID"
                              ItemsSource="{Binding PageModel.PageSelection.InventoryReportingLevel}" DisplayMemberPath="NAME"
                              SelectedValue="{Binding PageModel.PageCustomer.CustomerInfo.INV_RPT_LEVEL_ID}"></ComboBox>
                    </StackPanel>
                    <StackPanel Name="SpReportByPo" Orientation="Vertical" Grid.Row="2" Grid.Column="1" Margin="6,0,0,0">
                        <Label Name="LblReportByPo" Target="{Binding ElementName=TxtReportByPO}" Content="Do we report by PO?"></Label>
                        <CheckBox Name="ChkReportByPo" Style="{DynamicResource SliderCheckBox}" HorizontalAlignment="Left"
                              ></CheckBox>
                    </StackPanel>
                </Grid>
                <Grid Name="GridMiscellaneous" Grid.Column="0" Grid.Row="3">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"></RowDefinition>
                        <RowDefinition Height="*"></RowDefinition>
                        <RowDefinition Height="5"></RowDefinition>
                        <RowDefinition Height="*"></RowDefinition>
                        <RowDefinition Height="5"></RowDefinition>
                        <RowDefinition Height="*"></RowDefinition>
                    </Grid.RowDefinitions>
                    <TextBlock Grid.Row="0" Text="Other stuff"></TextBlock>
                    <StackPanel Name="SpSaveDirectory" Orientation="Vertical" Grid.Row="1" Margin="6,0,0,0">
                        <Label Name="LblSaveDirectory" Target="{Binding ElementName=TxtSaveDirectory}" Content="Where should saved reports go?"></Label>
                        <TextBox Name="TxtSaveDirectory" ></TextBox>
                    </StackPanel>
                    <StackPanel Name="SpFrequency" Orientation="Vertical" Grid.Row="3" Margin="6,0,0,0">
                        <Label Name="LblFrequency" Target="{Binding ElementName=TxtFrequency}"  Content="{Binding Path=Text, ElementName=CbCustomerName}" ContentStringFormat="How often do you run reports for {0}?"></Label>
                        <TextBox Name="TxtFrequency" ></TextBox>
                    </StackPanel>
                    <StackPanel Name="SpHoursPerReport" Orientation="Vertical" Grid.Row="5" Margin="6,0,0,0">
                        <Label Name="LblHoursPerReport" Target="{Binding ElementName=TxtHoursPerReport}" Content="How many hours do you spend per report (whole hours)?"></Label>
                        <TextBox Name="TxtHoursPerReport" ></TextBox>
                    </StackPanel>
                </Grid>
            </Grid>
            <Grid Name="GridCustomerPeopleAndTitles" Grid.Column="2">
                <Grid.RowDefinitions>
                    <RowDefinition Height="1.2*"></RowDefinition>
                    <RowDefinition Height="1.5*"></RowDefinition>
                    <RowDefinition Height=".8*"></RowDefinition>
                </Grid.RowDefinitions>
                <Grid Name="GridCustomerPeople" Grid.Row="0">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"></RowDefinition>
                        <RowDefinition Height="*"></RowDefinition>
                        <RowDefinition Height="*"></RowDefinition>
                        <RowDefinition Height="*"></RowDefinition>
                    </Grid.RowDefinitions>
                    <TextBlock Name="TxtbCustomerPeople" Grid.Row="0" Text="{Binding Path=Text, ElementName=CbCustomerName, StringFormat=These people also work with \{0\}}"></TextBlock>
                    <StackPanel Orientation="Vertical" Grid.Row="1" Margin="6,0,0,0">
                        <Label Name="LblSalesReps" Content="Sales Reps:"></Label>
                        <TextBlock Name="TxtbSalesReps" Text="{Binding PageModel.PageCustomer.SalesReps}" TextTrimming="CharacterEllipsis" MaxWidth="550"></TextBlock>
                    </StackPanel>
                    <StackPanel Orientation="Vertical" Grid.Row="2" Margin="6,0,0,0">
                        <Label Name="LblBillers" Content="Billers:"></Label>
                        <TextBlock Name="TxtbBillers" Text="{Binding PageModel.PageCustomer.Billers}" TextTrimming="CharacterEllipsis" MaxWidth="550"></TextBlock>
                    </StackPanel>
                    <StackPanel Orientation="Vertical" Grid.Row="3" Margin="6,0,0,0" HorizontalAlignment="Stretch">
                        <Label Name="LblCams" Content="Customer Account Managers:"></Label>
                        <TextBlock Name="TxtbCams" Text="{Binding PageModel.PageCustomer.CAMs}" TextTrimming="CharacterEllipsis"></TextBlock>
                    </StackPanel>
                </Grid>
                <Grid Name="GridTitleGroups" Grid.Row="1">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"></RowDefinition>
                        <RowDefinition Height="*"></RowDefinition>
                    </Grid.RowDefinitions>
                    <TextBlock Name="TxtbTitleGroups" Grid.Row="0" Text="{Binding Path=Text, ElementName=CbCustomerName, StringFormat=Group \{0\}\'s titles}"></TextBlock>
                    <Grid Grid.Row="1">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="15"></ColumnDefinition>
                            <ColumnDefinition Width="*"></ColumnDefinition>
                            <ColumnDefinition Width="10"></ColumnDefinition>
                            <ColumnDefinition Width="*"></ColumnDefinition>
                            <ColumnDefinition Width="15"></ColumnDefinition>
                        </Grid.ColumnDefinitions>
                        <ListBox Name="ListTitles" ItemsSource="{Binding}" Grid.Column="1" ToolTip="">

                        </ListBox>
                        <Border Grid.Column="3">
                            <Grid Name="GridTitleGroupItems">
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="30"></RowDefinition>
                                    <RowDefinition Height="*"></RowDefinition>
                                </Grid.RowDefinitions>
                                <Rectangle Grid.Row="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
                                       ToolTip=""></Rectangle>
                                <TextBlock Name="TxtbNewTitleGroup" Grid.Row="0" Text="Create a New Group" AllowDrop="True" HorizontalAlignment="Center"
                                 VerticalAlignment="Center" ToolTip=""></TextBlock>
                                <TreeView Name="TreeTitleGroups" ItemsSource="{Binding}" AllowDrop="True" Grid.Row="1" BorderBrush="{x:Null}" ToolTip="">

                                </TreeView>
                            </Grid>
                        </Border>
                    </Grid>
                </Grid>
                <Grid Name="GridCustomerGroups" Grid.Row="2">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"></RowDefinition>
                        <RowDefinition Height="*"></RowDefinition>
                    </Grid.RowDefinitions>
                    <TextBlock Name="TxtbCustomerGroups" Grid.Row="0" Text="{Binding Path=Text, ElementName=CbCustomerName, StringFormat=Group \{0\}\'s customers}"></TextBlock>
                </Grid>
            </Grid>
        </Grid>
    </Grid>

【讨论】:

  • 我已经粘贴了上面的代码。不幸的是,石灰区域没有填满整个可用区域。它在下拉列表的末尾停止。 pic here
  • 这证明了实际的网格布局有效。检查网格的父级 - 它必须将其 VerticalAlignment 设置为 Left 或者它可以是一个自定义面板,强制其子级占用尽可能小的空间。
  • 我终于找到了问题的原因并纠正了它,感谢这个答案排除了我的本地标记的问题。我认为,这对每个人都是有用的策略。
【解决方案2】:

我终于找到了导致内容大小不合适的原因。 ContentTemplate 隐藏在选项卡项样式中,将所有内容放置在水平 StackPanel 中。我将 StackPanel 切换为 Grid,瞧!正确的 wpf 布局。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-07
    • 2019-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-20
    • 2021-05-26
    • 1970-01-01
    相关资源
    最近更新 更多