代码之家  ›  专栏  ›  技术社区  ›  google dev

使WPF文本块只增长而不收缩?

  •  2
  • google dev  · 技术社区  · 7 年前

    在我的应用程序中,我设置 Text TextBlock 已命名 tbkStatus 很多次。

    我怎样才能 控件 只是自动增长以适应文本,而不是在文本更改时收缩?

    这个 StatusText 每隔几秒钟更改一次,就会出现长文本和短文本状态。 我想让我的文本块适合最长文本的大小,即使有短文本,文本块也不应收缩

    <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>
    
    3 回复  |  直到 7 年前
        1
  •  3
  •   Maciek Świszczowski    3 年前

    仅Xaml解决方案:

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

    只要 Width 生长, MinWidth 也在增长。因此,控件不能收缩。

        2
  •  0
  •   TheGeneral    7 年前

    我想你可以听听布局事件,比如 SizeChanged LayoutUpdated 或者写一些行为

    在下面的示例中,基本前提是倾听这些事件中的任何一个,并强制您的控制永不收缩

    笔记 这完全没有经过测试,只是一个想法,也许你可以设置 MinWidth 而不是属性

    Xaml

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

    代码隐藏

    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;
        }
    }
    

    另请注意

    这个 SizeChangedEventArgs 类有许多属性,您可以利用这些属性

        3
  •  -1
  •   Lion King    7 年前

    您可以这样做:

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

    自从 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
          }
    
    });
    

    或者,您可以使用 TextBox 并将其设为只读并使用 TextChanged 事件