wordpress   发布时间:2022-04-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了windows-8.1 – Windows应用程序确定TextBlock是否被修剪大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

我有一个具有固定高度/宽度的GridItem. 它包含一个具有最大行集的文本块. 如何确定此文本是否已修剪? 我想添加特殊功能,如果它被修剪. 旧方法 – 当TextWrapping设置为None时 要知道是否修剪了TextBlock,我们可以订阅它的SizeChanged事件并将其ActualWidth与您指定的MaxWidth进行比较.要获得TextBlock的正确ActualWidth,我们
我有一个具有固定高度/宽度的GridItem.

它包含一个具有最大行集的文本块.

如何确定此文本是否已修剪?
我想添加特殊功能,如果它被修剪.

方法 – 当textwrapping设置为None时

要知道是否修剪了TextBlock,我们可以订阅它的SizeChanged事件并将其ActualWidth与您指定的MaxWidth进行比较.要获得TextBlock的正确ActualWidth,我们需要将TextTrimming保留为其认值(即TextTrimming.NonE),并在宽度超过时将其设置为trimmed.

方法 – 当textwrapping设置为Wrap时

现在我知道因为textwrapping设置为Wrap并假设未指定VirticalAlignment(认为Stretch),Width将始终保持不变.当TextBlock的实际高度超过其父级的高度时,我们只需要监视SizeChanged事件.

让我们使用一个行为来封装上面的所有逻辑.这里需要提到的是,带有一堆附加属性的静态助手类或从TextBlock继承的新控件可以完全相同;但作为一个大混合粉丝,我更喜欢尽可能使用行为.

行为

public class TextBlockAutoTrimBehavior : DependencyObject,IBehavior
{
    public bool IStriR_418_11845@med
    {
        get { return (bool)GetValue(IStriR_418_11845@medProperty); }
        set { SETVALue(IStriR_418_11845@medProperty,value); }
    }

    public static readonly DependencyProperty IStriR_418_11845@medProperty =
        DependencyProperty.Register("IStriR_418_11845@med",typeof(bool),typeof(TextBlockAutoTrimBehavior),new PropertyMetadata(false));

    public DependencyObject AssociatedObject { get; set; }

    public void Attach(DependencyObject associatedObject)
    {
        this.AssociatedObject = associatedObject;
        var textBlock = (TextBlock)this.AssociatedObject;

        // subscribe to the SizeChanged event so we will kNow when the Width of the TextBlock goes over the MaxWidth
        textBlock.SizeChanged += TextBLOCK_SIZEChanged;
    }

    private void TextBLOCK_SIZEChanged(object sender,SizeChangedEventArgs E)
    {
        // ignore the first time height change
        if (e.PrevIoUsSize.Height != 0)
        {
            var textBlock = (TextBlock)sender;

            // notify the IStriR_418_11845@med dp so your viewmodel property will be notified via data binding
            this.IStriR_418_11845@med = true;
            // unsubscribe the event as we don't need it anymore
            textBlock.SizeChanged -= TextBLOCK_SIZEChanged;

            // then we trim the TextBlock
            textBlock.TextTrimming = TextTrimming.WordEllipsis;
        }
    }

    public void Detach()
    {
        var textBlock = (TextBlock)this.AssociatedObject;
        textBlock.SizeChanged += TextBLOCK_SIZEChanged;
    }
}

XAML

<Grid HorizontalAlignment="Center" Height="73" VerticalAlignment="Center" Width="200" BACkground="#FFD2A6A6" Margin="628,329,538,366">
    <TextBlock x:Name="myTextBlock" textwrapping="Wrap" Text="test" FontSize="29.333">
        <Interactivity:Interaction.behaviors>
            <local:TextBlockAutoTrimBehavior IStriR_418_11845@med="{Binding IStriR_418_11845@medInVm}" />
        </Interactivity:Interaction.behaviors>
    </TextBlock>
</Grid>

请注意,行为公开了依赖项属性IStriR_418_11845@med,您可以将数据绑定到viewmodel中的属性(在本例中为IStriR_418_11845@medInVm).

附:在WinRT中没有FormattedText函数,否则实现可能会有所不同.

大佬总结

以上是大佬教程为你收集整理的windows-8.1 – Windows应用程序确定TextBlock是否被修剪全部内容,希望文章能够帮你解决windows-8.1 – Windows应用程序确定TextBlock是否被修剪所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。