简体   繁体   English

使 WPF TextBlock 只增长而不缩小?

[英]Making a WPF TextBlock be only grow and not shrink?

In my app I set the Text of the TextBlock named tbkStatus many times.在我的应用程序中,我多次设置名为tbkStatusTextBlockText

How can I make the TextBlock be just grow auto to fit the text but not shrink when the text changed?如何让TextBlock自动增长以适应文本但在文本更改时不缩小?

The StatusText changes every few seconds, There are statuses with long text and short text. StatusText每隔几秒就会改变一次,有长文本和短文本的状态。 I want my TextBlock to fit itself to the size of the longest text that was, and even when there is a short text the TextBlock should not shrink我希望我的 TextBlock 适合最长文本的大小,即使有短文本,TextBlock 也不应该缩小

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Height="200" Width="400" 
        WindowStartupLocation="CenterScreen" 
        ResizeMode="CanMinimize" Topmost="True">
    <Window.Resources>

    </Window.Resources>
    <Grid >
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="AUTO" />
            <RowDefinition Height="AUTO" />
            <RowDefinition Height="AUTO" />
            <RowDefinition Height="AUTO" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <TextBlock Text="Please wait ..." Grid.Row="1" Margin="6"/>

        <TextBlock Name="tbkStatus" Grid.Row="2" Margin="6" TextWrapping="Wrap" Text="{Binding StatusText}"/>

        <ProgressBar Grid.Row="3" Margin="6" Height="20"/>
        <Button Grid.Row="4" HorizontalAlignment="Center" Padding="24,3" Margin="6" Content="Stop"/>
    </Grid>
</Window>

Xaml only solution: Xaml 唯一解决方案:

<TextBlock Text="{Binding StatusText}"
           MinWidth="{Binding RelativeSource={RelativeSource Self}, Path=ActualWidth}"
           HorizontalAlignment="Left"/>

This way whenever the Width growths, MinWidth growths too.这样,每当Width增长时, MinWidth也会增长。 Hence, the control can't shrink back.因此,控制不能收缩。

I guess you could just listen to Layout Events like SizeChanged or LayoutUpdated or write some sort of behavior我想你可以只听像SizeChangedLayoutUpdated这样的布局事件或编写某种行为

In the below example, the basic premise is to listen to either of these events, and force your control to never shrink在下面的示例中,基本前提是侦听这些事件中的任何一个,并强制您的控制权永不收缩

Note this is totally untested and was just an idea, maybe you could set the MinWidth Property instead请注意,这完全未经测试,只是一个想法,也许您可​​以改为设置MinWidth属性

Xaml xml

<TextBlock x:Name="tbkStatus" SizeChanged="OnSizeChanged"/>

Code Behind背后的代码

private double _lastSize;

private void OnSizeChanged(object sender, SizeChangedEventArgs e)
{
    var textBlock = sender as TextBlock;
    if (textBlock == null)
    {
        return;
    }

    if (e.WidthChanged)
    {
        if (textBlock.Width < _lastSize)
        {
            textBlock.Width = _lastSize;
        }
        _lastSize = textBlock.Width;
    }
}

Also Note另请注意

The SizeChangedEventArgs Class has many properties that you might be able to take advantage of SizeChangedEventArgs类具有许多您可以利用的属性

You can do something like this:你可以这样做:

 <TextBlock Name="tbkStatus" Grid.Row="2" Margin="6" TextWrapping="Wrap" Text="{Binding StatusText}"/>

Since TextBlock doesn't have TextChange event this will do that job由于TextBlock没有TextChange事件,这将完成这项工作

DependencyPropertyDescriptor dp = DependencyPropertyDescriptor.FromProperty(TextBlock.TextProperty, typeof(TextBlock));
int textLength =0 


dp.AddValueChanged(tbkStatus, (object a, EventArgs b) =>
{
      if (textLength < tbkStatus.Text.Length)
      {
       textLength = tkbStatus.Text.Length;
       tbkStatus.Width = textLength * SomeValue; //You have to play around and see what value suits you best since it depends on font and it's size
      }

});

Alternatively, you can use a TextBox and make it read only and use the TextChanged event.或者,您可以使用TextBox并使其只读并使用TextChanged事件。

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

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