wpf - 受 TextBox 文本影响的奇怪 WPF 网格行为

标签 wpf

我不明白为什么我的 TextBox 的内容会影响我的网格列宽。我设置了一个包含 3 列的网格,宽度定义为 50、*、50,如下所示

Grid Setup

现在,在使用时,中心列会随着 TextBox 中文本的变化而增大/缩小,请参见下面的 2 个示例。实际的 TextBox 并没有改变大小,所以我不明白为什么网格在改变。网格位于用户控件内的边框内。 UserControl 用作 ListBox 中的 DataTemplate。

Initial Grid with No input in the 1st TextBox

Grid Layout with long string in 1st TextBox

编辑:我发现这个问题与 ListBox 中的 UserControl 有关,请参见下面的示例图片(ListBox 中的 UserControl(红色圆圈)与 Form 上的 UserControl(蓝色圆圈)。网格行为正如预期的那样,当 UserControl 直接放在窗体上时。下面给出了窗体的代码。

UserControl in ListBox vs. UserControl Placed on Form

用户控件 XAML:

<Grid ShowGridLines="True">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="50"/>
            <ColumnDefinition Width="1*"/>
            <ColumnDefinition Width="50"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <TextBlock Text="Name:" Margin="2" />
        <TextBox Grid.Column="1" Margin="2" Text="{Binding Name, UpdateSourceTrigger=PropertyChanged}" Grid.ColumnSpan="2" />

        <TextBlock Text="Shift:" Grid.Row="1" Margin="2"  />
        <TextBox  Grid.Column="1" Grid.Row="1" Margin="2" Text="{Binding TimeShift, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, FallbackValue=0}" HorizontalContentAlignment="Right" />
        <TextBlock Text="s" Grid.Row="1" Grid.Column="2" Margin="2" />
    </Grid>

窗口/窗体 XAML:

<Window x:Class="CrashSimOffice.FileImport"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:CrashSimOffice"
    Title="File Import" Height="350" Width="450" Background="White" Icon="/CrashSimOffice;component/Images/16/page-white-save.png">
<Grid>

    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="75"/>
        <ColumnDefinition Width="75"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="75"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    <Button Content="Add" Margin="2" Command="{Binding AddFileCommand}" />
    <Button Content="Remove" Grid.Column="1" Margin="2" Command="{Binding RemoveFileCommand}" />

    <ListBox HorizontalContentAlignment="Stretch" Grid.ColumnSpan="4" Margin="2" Grid.Row="1" ItemsSource="{Binding Files}" SelectedItem="{Binding CurrentFile}"  ScrollViewer.CanContentScroll="False" Background="WhiteSmoke">
        <ListBox.ItemTemplate>
            <DataTemplate DataType="{x:Type local:FileViewModel}">
                <local:FileView DataContext="{Binding}" />
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

    <Button Content="Done" Grid.Column="3" Margin="2" Grid.Row="2" Click="Button_Click" />


    <local:FileView Grid.Row="3" Grid.ColumnSpan="4" />
</Grid>

最佳答案

好的,我想通了,Bruno V 你让我认为它一定与 ListBox 有关。我需要将其添加到我的列表框中:

ScrollViewer.Horizo​​ntalScrollBarVisibility="禁用"

不确定为什么会这样,但确实如此。

关于wpf - 受 TextBox 文本影响的奇怪 WPF 网格行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27509988/

相关文章:

c# - 关于将嵌套/分层对象的字段映射到平面列表的意见?

wpf - 为什么计算机进入休眠状态会扰乱我的 WPF 程序以及该怎么办

c# - 如何扩展 Entity Framework 6 模型

wpf - 使用 WPF 时如何消除菜单字体模糊?

C# 数据网格刷新

wpf - DependencyProperty.RegisterAttached 和多个实例

c# - 如何将用户控件内容控件中的属性绑定(bind)到属性?

c# - 非均匀分布数据的均匀分布点标记

c# - 10 秒后重定向到新的 wpf 窗口?

c# - 在 WPF 中创建 Windows.UI.Composition.Compositor 的实例会抛出 0x80070005 访问被拒绝