【问题标题】:WPF fixed rows are resizingWPF 固定行正在调整大小
【发布时间】:2016-01-28 19:14:45
【问题描述】:

我是 WPF 的新手,我尝试创建特定的 UserControl 来显示单个产品的数据。我在 UserControl 中使用了 Grid。所以我创建了 5 列和 3 行。我想让 4 列固定(图像、绿色、蓝色和带控件的列)和最后一列(橙色)以填充所有可用空间。这是我的 XAML 和一些屏幕截图:

<Grid Margin="0,0,0,5" Background="#FFDCD9D9" >
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="80" />
        <ColumnDefinition Width="70" />
        <ColumnDefinition Width="70" />
        <ColumnDefinition Width="70" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>

    <Image Grid.Column="0" Grid.Row="0" Grid.RowSpan="4" Source="{Binding ItemThumbnailUrl}" Stretch="None" HorizontalAlignment="Right" Margin="5,0"  />
    <StackPanel  Grid.Column="1" Grid.Row="0" Grid.ColumnSpan="4" Background="#FFDA6F6F">
        <Label  BorderThickness="0" Content="dsgsdgsgsgsdgsdg sd " FontSize="13.333" FontWeight="Bold"  HorizontalAlignment="Left" />
    </StackPanel>
    <StackPanel Grid.Column="1" Grid.Row="1" Orientation="Horizontal" Background="#FF517823" HorizontalAlignment="Left" Width="70">
        <Label  Content="{Binding ItemPrice}" HorizontalAlignment="Left" FontSize="9.333" Width="45" />
        <Label Content="грн." HorizontalAlignment="Left" FontSize="9.333" Width="25"/>
    </StackPanel>
    <StackPanel Grid.Column="2" Grid.Row="1" Orientation="Horizontal" Background="#FF214299" HorizontalAlignment="Left" Width="70">
        <Label  Content="{Binding Quantity}" HorizontalAlignment="Left" FontSize="9.333" Width="45" />
        <Label Content="шт." HorizontalAlignment="Left" FontSize="9.333" Width="25"/>
    </StackPanel>
    <StackPanel Grid.Column="1" Grid.Row="2" Orientation="Horizontal" Background="#FF88B91E" HorizontalAlignment="Left" Width="70">
        <Label  Content="1С" HorizontalAlignment="Right" FontSize="9.333" Foreground="#FF8B8888" Width="45"/>
        <Label Content="грн." HorizontalAlignment="Right" FontSize="9.333" Foreground="#FF8B8888" Width="25"/>
    </StackPanel>

    <StackPanel Grid.Column="2" Grid.Row="2" Orientation="Horizontal" Background="#FF228CBD" HorizontalAlignment="Left" Width="70">
        <Label  Content="1С" HorizontalAlignment="Right" FontSize="9.333" Foreground="#FF8B8888" Width="45"/>
        <Label Content="шт." HorizontalAlignment="Right" FontSize="9.333" Foreground="#FF8B8888" Width="25"/>
    </StackPanel>

    <CheckBox Grid.Column="3" Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center"/>
    <Button Grid.Column="3" Grid.Row="2" Background="{x:Null}" Content="Редакт." Foreground="#FF444343" Width="50" HorizontalAlignment="Center" VerticalAlignment="Center" />

    <Label Grid.Column="4" Grid.RowSpan="2" Grid.Row="1" Background="#FFE08212" HorizontalContentAlignment="Stretch"  />
</Grid>

如果我的“标题”文本(在红色单元格中)小于 3 个固定列的总和,则一切正常,但如果文本较大,我在列之间存在一些填充问题(请参见图片)

那么我该如何解决这个问题呢?

【问题讨论】:

  • 您是否尝试将堆栈面板的 ColumnSpan 设置为 5?
  • 你的意思是红色的 StackPanel 吗?
  • 是的。文字较大的那个。
  • 我刚试过,结果还是一样
  • 为什么堆栈面板中有标签?您可以直接给 Grid.ColumnSpan 标记自己。移除堆栈面板。如果您有两个或多个控件作为孩子,Stackpanel 会派上用场。

标签: c# wpf xaml


【解决方案1】:

当在 DataTemplate 中使用 Grid 时,我可以在各种情况下重现您的问题。我删除了StackPanel 并使用了TextBlock,然后使用了TextBlock 托管在单独的Grid 中,但结果都相同。我猜当 WPF 确定所需的大小时出现了问题。我经常在 Grids 中发生这种奇怪的行为(当 ItemTemplate 的一部分时)。如果您需要快速解决方法,那么这应该可以解决问题

                    <Grid.ColumnDefinitions>
                        <ColumnDefinition MinWidth="80" MaxWidth="80" />
                        <ColumnDefinition MinWidth="70" MaxWidth="70" />
                        <ColumnDefinition MinWidth="70" MaxWidth="70" />
                        <ColumnDefinition MinWidth="70" MaxWidth="70" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>

【讨论】:

    猜你喜欢
    • 2014-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-05
    相关资源
    最近更新 更多