将样式应用于 Silverlight 中 ContentPresenter 内的 TextBlock
如果我定义了以下样式:
<UserControl.Resources>
<Style TargetType="TextBlock" x:Key="ProblemStyle">
<Setter Property="FontSize" Value="40"/>
<Setter Property="FontWeight" Value="Bold"/>
</Style>
</UserControl.Resources>
然后,当我将 ContentPresenter 数据绑定到字符串时,在 WPF 中,我可以使用以下 XAML 根据需要设置文本样式:
<ContentPresenter Content="{Binding Problem}">
<ContentPresenter.Resources>
<Style TargetType="TextBlock" BasedOn="{StaticResource ProblemStyle}" />
</ContentPresenter.Resources>
</ContentPresenter>
但是,在 Silverlight 中,这不起作用。有没有一种方法对双方都有效?
If I have the following style defined:
<UserControl.Resources>
<Style TargetType="TextBlock" x:Key="ProblemStyle">
<Setter Property="FontSize" Value="40"/>
<Setter Property="FontWeight" Value="Bold"/>
</Style>
</UserControl.Resources>
Then when I have a ContentPresenter data bound to a string, in WPF I can get it to style the text as required with the following XAML:
<ContentPresenter Content="{Binding Problem}">
<ContentPresenter.Resources>
<Style TargetType="TextBlock" BasedOn="{StaticResource ProblemStyle}" />
</ContentPresenter.Resources>
</ContentPresenter>
However, in Silverlight, this doesn't work. Is there a way that works for both?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 TextElement Attached 属性。您将无法设置样式,但影响文本块的大多数属性都在那里。
Use the TextElement Attached property. You will not be able to set a style, but most of the properties that effects the Textblock are there..
第一的:
确保在应用程序尝试呈现 ContentPresenter 之前加载您的样式“ProblemStyle”。在 Silverlight 中,定义样式的顺序会有所不同,如果没有首先加载它,那么它可能不会读取任何内容。
好的,我将在这里进行一些假设,第一个假设是您正在使用 ContentControl 来显示某些内容,并且 ContentPresenter 位于该控件内部。
但为什么不为 ContentControl 创建一个样式呢?
然后您的 ContentControl 会将 Style 设置为“ProblemStyle”的 StaticResource。
因为默认情况下,ContentControl 的模板具有 ContentPresenter - 或者您也可以在样式中定义 ContentPresenter 模板:
该模板只是作为占位符,用于说明它的位置。
First:
Make sure that your style "ProblemStyle" is being loaded before the application tries to render the ContentPresenter. In Silverlight, the order the styles are defined makes a difference, and if it has not been loaded first then it may not be reading anything.
Ok, I am going to run on some assumptions here, the first one being that you are using a ContentControl to display something and that the ContentPresenter is inside of this control.
But why not create a Style for the ContentControl?
Then your ContentControl would have the Style set to the StaticResource of "ProblemStyle".
Since by default the template of a ContentControl has the ContentPresenter - or you can define the ContentPresenter template in the style as well:
The template there is just as a placeholder to give an idea of where it would/could be located.