使 WPF 文本框的宽度仅与它所拥有的空间一样宽,但会随着空间的扩展而扩展?

发布于 2024-11-07 11:06:44 字数 4387 浏览 1 评论 0原文

请注意文本框如何向右扩展,直到有足够的水平空间来容纳内容?好吧,我希望它不要扩展文本并使文本适合窗口中的空间。

在此处输入图像描述

如果窗口展开,则其所在的 Grid.Column 将展开,但文本框本身应展开为合身。够简单吗?

有什么建议吗?这是我第一次尝试 WPF,到目前为止,一切都非常顺利。

编辑:这是我的 XAML 标记:

<Window x:Class="GameLenseWpf.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="450" Width="350" MinHeight="450" MinWidth="350">

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="0.15*" />
            <RowDefinition />
        </Grid.RowDefinitions>
        <Image Grid.Row="0" Stretch="Fill" Source="Image/topBarBg.png" />
        <StackPanel Orientation="Horizontal" Grid.Row="0">
            <TextBlock Text="Platform" 
                       Foreground="White" 
                       FontFamily="Georgia"
                       FontSize="15" 
                       Margin="10"
                       HorizontalAlignment="Center"
                       VerticalAlignment="Center"/>
            <ComboBox x:Name="cmbPlatform" 
                      Margin="10"
                      FontFamily="Georgia"
                      FontSize="15"
                      MinHeight="30"
                      MinWidth="140"
                      VerticalAlignment="Center"
                      VerticalContentAlignment="Center" SelectionChanged="cmbPlatform_SelectionChanged">
                <ComboBoxItem>All Platforms</ComboBoxItem>
                <ComboBoxItem>Playstation 3</ComboBoxItem>
                <ComboBoxItem>XBox 360</ComboBoxItem>
                <ComboBoxItem>Wii</ComboBoxItem>
                <ComboBoxItem>PSP</ComboBoxItem>
                <ComboBoxItem>DS</ComboBoxItem>
            </ComboBox>
        </StackPanel>
        <Image x:Name="imgAbout" Grid.Row="0" Source="Image/about.png" 
               Height="16" HorizontalAlignment="Right"
               VerticalAlignment="Center"
               Margin="0 0 10 0"    />
        <ListBox Grid.Row="1" x:Name="lstGames" Background="#343434" Padding="5">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid Height="120" Margin="0 10">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="90"/>
                            <ColumnDefinition />
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition />
                        </Grid.RowDefinitions>

                        <Border BorderBrush="#202020" BorderThickness="5" CornerRadius="4" Panel.ZIndex="0">
                            <Image Grid.Row="0" Grid.Column="0" Source="{Binding ImageUrl}" Stretch="Fill"/>                            
                        </Border>

                        <StackPanel Grid.Row="0" Grid.Column="1" Margin="12 0 0 0">
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="Title:" FontFamily="Arial" Foreground="White"/>
                                <TextBlock Text="{Binding Title}" FontFamily="Arial" Foreground="White" />
                            </StackPanel>
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="Release Date:" FontFamily="Arial" Foreground="White" />
                                <TextBlock Text="{Binding ReleaseDate}" FontFamily="Arial" Foreground="White" />
                            </StackPanel>
                            <TextBlock Text="Synopsis" FontFamily="Arial" Foreground="White" />
                            <TextBox Background="#454545" Text="{Binding Synopsis}" MinHeight="76" />
                        </StackPanel>  
                    </Grid>                    
                </DataTemplate>
            </ListBox.ItemTemplate>            
        </ListBox>
    </Grid>
</Window>

Notice how the textbox expands to the right until it has enough horizontal space to fit the content? Well I'd like it to not expand and fit the text with the space it has in the window.

enter image description here

If the windows expands, then the Grid.Column it's in will expand, but the textbox itself should expand to fit. Simple enough?

Any suggestions? This is my first foray into WPF and so far it's been pretty sleek.

Edit: Here's my XAML markup:

<Window x:Class="GameLenseWpf.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="450" Width="350" MinHeight="450" MinWidth="350">

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="0.15*" />
            <RowDefinition />
        </Grid.RowDefinitions>
        <Image Grid.Row="0" Stretch="Fill" Source="Image/topBarBg.png" />
        <StackPanel Orientation="Horizontal" Grid.Row="0">
            <TextBlock Text="Platform" 
                       Foreground="White" 
                       FontFamily="Georgia"
                       FontSize="15" 
                       Margin="10"
                       HorizontalAlignment="Center"
                       VerticalAlignment="Center"/>
            <ComboBox x:Name="cmbPlatform" 
                      Margin="10"
                      FontFamily="Georgia"
                      FontSize="15"
                      MinHeight="30"
                      MinWidth="140"
                      VerticalAlignment="Center"
                      VerticalContentAlignment="Center" SelectionChanged="cmbPlatform_SelectionChanged">
                <ComboBoxItem>All Platforms</ComboBoxItem>
                <ComboBoxItem>Playstation 3</ComboBoxItem>
                <ComboBoxItem>XBox 360</ComboBoxItem>
                <ComboBoxItem>Wii</ComboBoxItem>
                <ComboBoxItem>PSP</ComboBoxItem>
                <ComboBoxItem>DS</ComboBoxItem>
            </ComboBox>
        </StackPanel>
        <Image x:Name="imgAbout" Grid.Row="0" Source="Image/about.png" 
               Height="16" HorizontalAlignment="Right"
               VerticalAlignment="Center"
               Margin="0 0 10 0"    />
        <ListBox Grid.Row="1" x:Name="lstGames" Background="#343434" Padding="5">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid Height="120" Margin="0 10">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="90"/>
                            <ColumnDefinition />
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition />
                        </Grid.RowDefinitions>

                        <Border BorderBrush="#202020" BorderThickness="5" CornerRadius="4" Panel.ZIndex="0">
                            <Image Grid.Row="0" Grid.Column="0" Source="{Binding ImageUrl}" Stretch="Fill"/>                            
                        </Border>

                        <StackPanel Grid.Row="0" Grid.Column="1" Margin="12 0 0 0">
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="Title:" FontFamily="Arial" Foreground="White"/>
                                <TextBlock Text="{Binding Title}" FontFamily="Arial" Foreground="White" />
                            </StackPanel>
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="Release Date:" FontFamily="Arial" Foreground="White" />
                                <TextBlock Text="{Binding ReleaseDate}" FontFamily="Arial" Foreground="White" />
                            </StackPanel>
                            <TextBlock Text="Synopsis" FontFamily="Arial" Foreground="White" />
                            <TextBox Background="#454545" Text="{Binding Synopsis}" MinHeight="76" />
                        </StackPanel>  
                    </Grid>                    
                </DataTemplate>
            </ListBox.ItemTemplate>            
        </ListBox>
    </Grid>
</Window>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风渺 2024-11-14 11:06:44

要使 TextBox 包裹在 ListBox 内,您可以进行以下更改:

  1. 使用 Horizo​​ntalContentAlignment="Stretch" 将列表框的内容设置为等于列表框的宽度。
  2. 禁用列表框的水平滚动条,以防止列表框获得所需的控件大小并防止文本框中自动换行。
  3. 在 TextBox 中设置 TextWrapping="Wrap"

以下是 XAML:

<ListBox Grid.Row="1" x:Name="lstGames" Background="#343434" Padding="5" 
         ScrollViewer.HorizontalScrollBarVisibility="Disabled"
         HorizontalContentAlignment="Stretch" >
</ListBox>

<TextBox Text="{Binding Synopsis}" MinHeight="76" TextWrapping="Wrap" />

To get a TextBox to wrap inside a ListBox you can make the following changes:

  1. Set the content of the listbox equal to the width of the listbox using: HorizontalContentAlignment="Stretch".
  2. Disable the horizontal scrollbar of the listbox to prevent listbox from getting the desired size of the controls and preventing the word wrap in your textbox.
  3. Set TextWrapping="Wrap" in the TextBox

Here is the XAML:

<ListBox Grid.Row="1" x:Name="lstGames" Background="#343434" Padding="5" 
         ScrollViewer.HorizontalScrollBarVisibility="Disabled"
         HorizontalContentAlignment="Stretch" >
</ListBox>

<TextBox Text="{Binding Synopsis}" MinHeight="76" TextWrapping="Wrap" />
笔芯 2024-11-14 11:06:44

我相信您需要设置文本框控件的 Margin 属性。在设计器中,您可以看到每个文本框(以及当您聚焦它们时的每个控件)周围的小圆圈。单击文本框右侧的小圆圈,使该控件随着当前布局控件中的可用空间而略微增长(通过单击圆圈,边距将添加到 XAML 中)。

我不知道您的图像中是否已经调整了窗口大小,但是对于该图像,您还需要设置文本框的宽度。

这有帮助吗?

I believe you need to set the Margin property of your textbox control. In the designer, you can see little circles around each textbox (and each control when you focus them, for that matter). Click the little circle on the right side of the textbox, to make that control grow marginally with the available space in the current layout control (by clicking the circle, the margin will be added into the XAML).

I don't know if in your image you've already adjusted the window size, but with that image it appears you'll also need to set the width for your textbox.

Does this help?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文