WPF:ContentPresenter上的TextTrimming [英] WPF: TextTrimming on a ContentPresenter

查看:526
本文介绍了WPF:ContentPresenter上的TextTrimming的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种简单的方法可以使TextTrimming与ContentPresenter一起使用?



我有TextBlock和AccessText的隐式样式,这些样式的TextTrimming设置为CharacterEllipsis,但ContentPresenter却没有。我可以将ContentPresenter更改为AccessText或TextBlock并将其设置在那里,但是模板仅处理文本内容。



有什么建议吗?



谢谢!

解决方案

从UIElement而不是Control派生的元素的隐式样式未应用如果元素是在控件的模板中定义的,则除非,隐式样式是在应用程序资源中定义的。对于ContentPresenter使用的TextBlock,也是如此。



例如,在以下XAML中,最终用于显示按钮内容的TextBlock将获取隐式样式:

 < Window.Resources> 
< Style TargetType = TextBlock>
< Setter Property = Foreground Value = Red />
< / Style>
< /Window.Resources>
< StackPanel>
< Button Content =不会变成红色 />
< TextBlock Text =将是红色的 />
< / StackPanel>

如果您采用完全相同的样式并将其移至应用程序的资源,则两者均为红色:

 < Application.Resources> 
< Style TargetType = TextBlock>
< Setter Property = Foreground Value = Red />
< / Style>
< /Application.Resources>

因此,您可以将隐式Style移至应用程序资源,这通常不是一个好主意。或者,您可以针对特定情况自定义显示。这可以包括添加隐式DataTemplate或自定义控件的模板。



如果您可以提供更多信息,那么会更容易知道哪种方法是最好的。 / p>

Is there a simple way to just get TextTrimming to work with a ContentPresenter?

I have implict styles for TextBlock and AccessText that have TextTrimming set to CharacterEllipsis, but it's not picked up by the ContentPresenter. I can change the ContentPresenter to an AccessText or TextBlock and set it there, but then the template only handles text content.

Any suggestions?

Thanks!

解决方案

Implicit Styles for elements that derive from UIElement, but not Control, are not applied if the element is defined in a control's Template unless the implict Style is defined in the application Resources. The same holds true for TextBlocks used by ContentPresenter.

For example, in the following XAML the TextBlock that is ultimately used to present the button's content will not get the implicit Style:

<Window.Resources>
    <Style TargetType="TextBlock">
        <Setter Property="Foreground" Value="Red" />
    </Style>
</Window.Resources>
<StackPanel>
    <Button Content="Will not be red" />
    <TextBlock Text="Will be red" />
</StackPanel>

If you take that exact same Style and move it to the application's Resources, then both will be red:

<Application.Resources>
    <Style TargetType="TextBlock">
        <Setter Property="Foreground" Value="Red" />
    </Style>
</Application.Resources>

So you can either move your implicit Style to application resources, which is generally not a good idea. Or you can customize the display for the specific scenario you have. This can include adding an implicit DataTemplate, or customizing a control's Template.

If you can provide more information, then it would be easier to know which is the best approach.

这篇关于WPF:ContentPresenter上的TextTrimming的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆