将样式应用于 Silverlight 中 ContentPresenter 内的 TextBlock

发布于 2024-09-24 08:42:43 字数 694 浏览 9 评论 0原文

如果我定义了以下样式:

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

晚风撩人 2024-10-01 08:42:44

使用 TextElement Attached 属性。您将无法设置样式,但影响文本块的大多数属性都在那里。

<ContentPresenter x:Name="ContentPresenter"
                              ContentSource="Header"
                              HorizontalAlignment="Left"
                              TextElement.FontFamily="Segoe UI"
                              TextElement.FontSize="12"
                              TextElement.FontWeight="Bold"
                              TextElement.Foreground="White"
                              RecognizesAccessKey="True" />

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 x:Name="ContentPresenter"
                              ContentSource="Header"
                              HorizontalAlignment="Left"
                              TextElement.FontFamily="Segoe UI"
                              TextElement.FontSize="12"
                              TextElement.FontWeight="Bold"
                              TextElement.Foreground="White"
                              RecognizesAccessKey="True" />
未蓝澄海的烟 2024-10-01 08:42:44

第一的:
确保在应用程序尝试呈现 ContentPresenter 之前加载您的样式“ProblemStyle”。在 Silverlight 中,定义样式的顺序会有所不同,如果没有首先加载它,那么它可能不会读取任何内容。

好的,我将在这里进行一些假设,第一个假设是您正在使用 ContentControl 来显示某些内容,并且 ContentPresenter 位于该控件内部。

但为什么不为 ContentControl 创建一个样式呢?

<Style x:key="ProblemStyle" TargetType="ContentControl">
  <Setter Property="FontSize" Value="40"/>
  <Setter Property="FontWeight" Value="Bold"/>
</Style>

然后您的 ContentControl 会将 Style 设置为“ProblemStyle”的 StaticResource。

因为默认情况下,ContentControl 的模板具有 ContentPresenter - 或者您也可以在样式中定义 ContentPresenter 模板:

<Style x:key="ProblemStyle" TargetType="ContentControl">
  <Setter Property="FontSize" Value="40"/>
  <Setter Property="FontWeight" Value="Bold"/>
  <Setter Property="Template">
    <Setter.Value>
       <ControlTemplate TargetType="ContentControl">
          <Border>
             <ContentPresenter Content="{TemplateBinding Content}"/>
          </Border>
       </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

该模板只是作为占位符,用于说明它的位置。

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?

<Style x:key="ProblemStyle" TargetType="ContentControl">
  <Setter Property="FontSize" Value="40"/>
  <Setter Property="FontWeight" Value="Bold"/>
</Style>

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:

<Style x:key="ProblemStyle" TargetType="ContentControl">
  <Setter Property="FontSize" Value="40"/>
  <Setter Property="FontWeight" Value="Bold"/>
  <Setter Property="Template">
    <Setter.Value>
       <ControlTemplate TargetType="ContentControl">
          <Border>
             <ContentPresenter Content="{TemplateBinding Content}"/>
          </Border>
       </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

The template there is just as a placeholder to give an idea of where it would/could be located.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文