热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

对齐控件中的文本底部-Alignbottomsoftextincontrols

Thefollowingsnippet:以下片段:<Windowx:xmlnshttp:schemas.microsoft.comw

The following snippet:

以下片段:


    
                    
            
    

Renders the following:
alt text

呈现以下内容:

Is there any way I can set in the Labels' styles so that their text bottoms should be aligned?
I have the same question with TextBlocks as well.

有没有什么方法可以设置标签的样式,以便他们的文本底部应该对齐?我对TextBlocks也有同样的问题。

NOTE: since I've been struggling with this issue for a while, please post only certains answers that you know that work.
I already tried: VerticalAlignment, VerticalContentAlignment, Padding, Margin. Is there anything else I am not aware of?

注意:由于我一直在努力解决这个问题,请发布您知道可行的某些答案。我已经尝试过:VerticalAlignment,VerticalContentAlignment,Padding,Margin。还有什么我不知道的吗?

I've read this post, but it doesn't talk about a scenario of different font size.

我已经阅读了这篇文章,但它没有谈到不同字体大小的情况。

UPDATE: The problem is, that even Padding is set to 0 there is still an indeterminate space around the font, within the ContentPresenter area. this space varies on the font size. If I could control this space I would be in a better situation.

更新:问题是,即使Padding设置为0,在ContentPresenter区域内的字体周围仍然存在不确定的空间。这个空间因字体大小而异。如果我可以控制这个空间,我会处于更好的状态。

Thanks

谢谢

6 个解决方案

#1


6  

There is no XAML only solution, you have to use code behind. Also, even with code-behind, there's no general solution for this, because what if your text is multi-line? Which baseline should be used in that case? Or what if there are multiple text elements in your template? Such as a header and a content, or more, which baseline then?

没有XAML唯一的解决方案,您必须使用后面的代码。此外,即使使用代码隐藏,也没有通用的解决方案,因为如果你的文字是多行的呢?在这种情况下应该使用哪个基线?或者,如果模板中有多个文本元素,该怎么办?如标题和内容,或更多,哪个基线呢?

In short, your best bet is to align the text manually using top/bottom margins.

简而言之,您最好的选择是使用上/下边距手动对齐文本。

If you're willing to make the assumption that you have a single text element, you can figure out the pixel distance of the baseline from the top of the element by instantiating a FormattedText object with all the same properties of the existing text element. The FormattedText object has a double Baseline property which holds that value. Note that you still would have to manually enter a margin, because the element might not sit exactly against the top or bottom of its container.

如果您愿意假设您有一个文本元素,则可以通过实例化具有现有文本元素的所有相同属性的FormattedText对象来计算基线与元素顶部的像素距离。 FormattedText对象具有双Baseline属性,该属性保存该值。请注意,您仍然必须手动输入边距,因为该元素可能不会完全位于其容器的顶部或底部。

See this MSDN forum post: Textbox Baseline

看到这篇MSDN论坛帖子:Textbox Baseline

Here's a method I wrote that extracts that value. It uses reflection to get the relevant properties because they are not common to any single base class (they are defined separately on Control, TextBlock, Page, TextElement and maybe others).

这是我写的一个提取该值的方法。它使用反射来获取相关属性,因为它们不是任何单个基类共有的(它们在Control,TextBlock,Page,TextElement和其他基础上分别定义)。

public double CalculateBaseline(object textObject)
{
    double r = double.NaN;
    if (textObject == null) return r;

    Type t = textObject.GetType();
    BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.Public;

    var fOntSizeFI= t.GetProperty("FontSize", bindingFlags);
    if (fOntSizeFI== null) return r;
    var fOntFamilyFI= t.GetProperty("FontFamily", bindingFlags);
    var fOntStyleFI= t.GetProperty("FontStyle", bindingFlags);
    var fOntWeightFI= t.GetProperty("FontWeight", bindingFlags);
    var fOntStretchFI= t.GetProperty("FontStretch", bindingFlags);

    var fOntSize= (double)fontSizeFI.GetValue(textObject, null);
    var fOntFamily= (FontFamily)fontFamilyFI.GetValue(textObject, null);
    var fOntStyle= (FontStyle)fontStyleFI.GetValue(textObject, null);
    var fOntWeight= (FontWeight)fontWeightFI.GetValue(textObject, null);
    var fOntStretch= (FontStretch)fontStretchFI.GetValue(textObject, null);

    var typeFace = new Typeface(fontFamily, fontStyle, fontWeight, fontStretch);

    var formattedText = new FormattedText(
        "W", 
        CultureInfo.CurrentCulture, 
        FlowDirection.LeftToRight, 
        typeFace, 
        fontSize, 
        Brushes.Black);

    r = formattedText.Baseline;

    return r;
}

EDIT: Shimmy, in response to your comment, I don't believe you've actually tried this solution, because it works. Here's an example:

编辑:Shimmy,回应你的评论,我不相信你真的尝试过这个解决方案,因为它有效。这是一个例子:

Example Baseline Alignment

Here's the XAML:

这是XAML:


    
        
    
    
        
        
    
    
        
        
    
    
        
        
    

And here's the code behind that achieves this

这是实现这一目标的背后代码

double baseRef = CalculateBaseline(tbref);
double base1 = CalculateBaseline(tb1) - baseRef;
double base2 = CalculateBaseline(tb2) - baseRef;
double base3 = CalculateBaseline(tb3) - baseRef;
tb1.Margin = new Thickness(0, 40 - base1, 0, 0);
tb2.Margin = new Thickness(0, 40 - base2, 0, 0);
tb3.Margin = new Thickness(0, 40 - base3, 0, 0);

#2


23  

Another fairly simple solution:

另一个相当简单的解

1) Use TextBlock controls instead of Labels. The reason being that TextBlock is lighter weight than Label - see http://joshsmithonwpf.wordpress.com/2007/07/04/differences-between-label-and-textblock/

1)使用TextBlock控件而不是标签。原因是TextBlock比Label更轻 - 请参阅http://joshsmithonwpf.wordpress.com/2007/07/04/differences-between-label-and-textblock/

2) Use the LineHeight and LineStackingStrategy = BlockLineHeight for your TextBlock style. This will align the two at their baseline easily.

2)使用LineHeight和LineStackingStrategy = BlockLineHeight作为TextBlock样式。这将使两者在基线处轻松对齐。


    
        
                
    
    

#3


5  

I really like the creative solutions that are presented here but I do think that in the long run (pun intended) we should use this:

我真的很喜欢这里展示的创意解决方案,但我认为从长远来看(双关语)我们应该使用这个:


   What
   ever
   FontSize

The only thing that is missing from the Run element is databinding of the Text property but that might be added sooner or later.

Run元素中唯一缺少的是Text属性的数据绑定,但可能迟早会添加。

A Run will not fix the alignment of labels and their textboxes but for many simple situation the Run will do quite nicely.

Run不会修复标签及其文本框的对齐方式,但对于许多简单的情况,Run会做得很好。

#4


2  


Small
Big

This should works well. Experiment with Baseline/Bottom/Center/Top.

这应该很好。使用Baseline / Bottom / Center / Top进行实验。

#5


1  

XAML designer supports aligning TextBlock controls by baseline at design time:

XAML设计器支持在设计时按基线对齐TextBlock控件:

enter image description here

This assigns fixed margins to your controls. As long as font sizes do not change at run time, the alignment will be preserved.

这会为您的控件分配固定边距。只要字体大小在运行时不会更改,就会保留对齐方式。

#6


0  

I actually found a simple answer based on Aviad's.

我实际上找到了一个基于Aviad的简单答案。

I created a converter that contains Aviad's function that accepts the element itself and returns calculated Thickness.

我创建了一个包含Aviad函数的转换器,该函数接受元素本身并返回计算的厚度。

Then I set up

然后我成立了


The disadvantage is that this is obviously occupies the original Margin property, since a TextBlock doesn't have a template so we can't set it via TemplateBinding.

缺点是这显然占用了原始Margin属性,因为TextBlock没有模板,所以我们无法通过TemplateBinding设置它。


推荐阅读
  • Spring常用注解(绝对经典),全靠这份Java知识点PDF大全
    本文介绍了Spring常用注解和注入bean的注解,包括@Bean、@Autowired、@Inject等,同时提供了一个Java知识点PDF大全的资源链接。其中详细介绍了ColorFactoryBean的使用,以及@Autowired和@Inject的区别和用法。此外,还提到了@Required属性的配置和使用。 ... [详细]
  • 本文讨论了微软的STL容器类是否线程安全。根据MSDN的回答,STL容器类包括vector、deque、list、queue、stack、priority_queue、valarray、map、hash_map、multimap、hash_multimap、set、hash_set、multiset、hash_multiset、basic_string和bitset。对于单个对象来说,多个线程同时读取是安全的。但如果一个线程正在写入一个对象,那么所有的读写操作都需要进行同步。 ... [详细]
  • 本文介绍了pack布局管理器在Perl/Tk中的使用方法及注意事项。通过调用pack()方法,可以控制部件在显示窗口中的位置和大小。同时,本文还提到了在使用pack布局管理器时,应注意将部件分组以便在水平和垂直方向上进行堆放。此外,还介绍了使用Frame部件或Toplevel部件来组织部件在窗口内的方法。最后,本文强调了在使用pack布局管理器时,应避免在中间切换到grid布局管理器,以免造成混乱。 ... [详细]
  • 带添加按钮的GridView,item的删除事件
    先上图片效果;gridView无数据时显示添加按钮,有数据时,第一格显示添加按钮,后面显示数据:布局文件:addr_manage.xml<?xmlve ... [详细]
  • Introduction(简介)Forbeingapowerfulobject-orientedprogramminglanguage,Cisuseda ... [详细]
  • 前端开发工程师必读书籍有哪些值得推荐?我们直接进入代码复杂版式设置,如下所示,先写些标签,源码在这个链接里面:https://codepen.io/Shadid ... [详细]
  • XML介绍与使用的概述及标签规则
    本文介绍了XML的基本概念和用途,包括XML的可扩展性和标签的自定义特性。同时还详细解释了XML标签的规则,包括标签的尖括号和合法标识符的组成,标签必须成对出现的原则以及特殊标签的使用方法。通过本文的阅读,读者可以对XML的基本知识有一个全面的了解。 ... [详细]
  • HDFS2.x新特性
    一、集群间数据拷贝scp实现两个远程主机之间的文件复制scp-rhello.txtroothadoop103:useratguiguhello.txt推pushscp-rr ... [详细]
  • Android系统移植与调试之如何修改Android设备状态条上音量加减键在横竖屏切换的时候的显示于隐藏
    本文介绍了如何修改Android设备状态条上音量加减键在横竖屏切换时的显示与隐藏。通过修改系统文件system_bar.xml实现了该功能,并分享了解决思路和经验。 ... [详细]
  • Android开发实现的计时器功能示例
    本文分享了Android开发实现的计时器功能示例,包括效果图、布局和按钮的使用。通过使用Chronometer控件,可以实现计时器功能。该示例适用于Android平台,供开发者参考。 ... [详细]
  • 在Xamarin XAML语言中如何在页面级别构建ControlTemplate控件模板
    本文介绍了在Xamarin XAML语言中如何在页面级别构建ControlTemplate控件模板的方法和步骤,包括将ResourceDictionary添加到页面中以及在ResourceDictionary中实现模板的构建。通过本文的阅读,读者可以了解到在Xamarin XAML语言中构建控件模板的具体操作步骤和语法形式。 ... [详细]
  • 本文讨论了如何在codeigniter中识别来自angularjs的请求,并提供了两种方法的代码示例。作者尝试了$this->input->is_ajax_request()和自定义函数is_ajax(),但都没有成功。最后,作者展示了一个ajax请求的示例代码。 ... [详细]
  • Tkinter Frame容器grid布局并使用Scrollbar滚动原理
    本文介绍了如何使用Tkinter实现Frame容器的grid布局,并通过Scrollbar实现滚动效果。通过将Canvas作为父容器,使用滚动Canvas来滚动Frame,实现了在Frame中添加多个按钮,并通过Scrollbar进行滚动。同时,还介绍了更新Frame大小和绑定滚动按钮的方法,以及配置Scrollbar的相关参数。 ... [详细]
  • CSS|网格-行-结束属性原文:https://www.gee ... [详细]
  • 关于extjs开发实战pdf的信息
    本文目录一览:1、extjs实用开发指南2、本 ... [详细]
author-avatar
惜泽2502852447
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有