English 中文(简体)
2. 如何界定所有锁定元素与相同的颜色
原标题:How to define all textblock elements the same color
  • 时间:2011-10-24 13:11:52
  •  标签:
  • wpf
  • styles

We are using global styles definitions for most of the types. We define then in the app.xaml file. When using TextBlock it is a problem to define a foreground color because it changes all the controls using TextBlock (Button s content color for example). How can we define a global style which will act only on specific TextBlock usages?

目前存在的问题使用:

<Style TargetType={x:Type TextBlock}>
  <Setter Property="Foreground" Value="Red"/>
</Style>
问题回答

由于我不认为有办法区分“your”(>TextBlock和其他控制的一部分,你的选择非常有限。

  • You could create named Style and add Style="{StaticResource coloredTextBlock}" or Foreground="{StaticResource textBlockColor}" to all TextBlocks. This would be quite tedious and non-DRY.
  • You could create your own type that inherits from TextBlock and style that. This has some of the disadvantages of the above solution (you have to remember doing that). But it has much less repetition.

这是因为:Content Presenter为显示内容创造了一个文本Block,而且由于文本Block在视觉树中,它将研究应用水平的资源。 如果你在申请一级界定了文本Block的风格,则将适用于控制范围内的这些文本Block。

一项工作是界定<代码>DataTemplate>>,用于<代码>System.String,其中我们可以明确使用默认文本Block显示内容。 你可以把该数据模板放在同一个词典中,你界定了文本Block风格,以便这一数据模板将适用于你风格的任何内容。

在你的申请资源中加上这一点,它应当为你工作——

<DataTemplate DataType="{x:Type system:String}">
  <TextBlock Text="{Binding}">
    <TextBlock.Resources>
      <Style TargetType="{x:Type TextBlock}"/>
    </TextBlock.Resources>
  </TextBlock>
</DataTemplate>

如果尚未申报,可在您的xaml中申报一个名称空间,

xmlns:system="clr-namespace:System;assembly=mscorlib"

www.un.org/Depts/DGACM/index_spanish.htm EDIT :检查其工作的样本。

<Style TargetType="{x:Type TextBlock}">
   <Setter Property="Foreground" Value="Red"/>
</Style>

<DataTemplate DataType="{x:Type system:String}">
  <TextBlock Text="{Binding}">
     <TextBlock.Resources>
        <Style TargetType="{x:Type TextBlock}"/>
     </TextBlock.Resources>
  </TextBlock>
</DataTemplate>

<Style TargetType="{x:Type Button}">
  <Setter Property="Foreground" Value="Yellow"/>
</Style>

<Style TargetType="{x:Type Label}">
  <Setter Property="Foreground" Value="Blue"/>
</Style>

仅举一,:如:

<Style x:Key="stRedTextBlock" TargetType={x:Type TextBlock}>
        <Setter Property="Foreground" Value="Red"/>
</Style>

并提及文本Block控制风格中的关键,在这种方式中,你一直要求这种特定文本Block风格,例如:

<TextBlock Name="textBlock1" Style="{StaticResource stRedTextBlock}" />




相关问题
Creating a Style in code behind

Does anyone know how to create a wpf Style in code behind, I can t find anything on the web or MSDN docs. I have tried this but it is not working: Style s = new Style(typeof(TextBlock)); s....

WPF Custom Themes

I have a simple question which is giving me some difficulty. I have downloaded a custom them for WPF of the net. Now i want to apply this theme to my App instead of the default one. How do i do that,...

Is it sometimes bad to use <BR />?

Is it sometimes bad to use <BR/> tags? I ask because some of the first advice my development team gave me was this: Don t use <BR/> ; instead, use styles. But why? Are there negative ...

WPF - How to apply effect to a cropped image?

I have an Image being clipped like so: <Image Width="45" Grid.Column="0" Source="{Binding Photo}"> <Image.Clip> <RectangleGeometry Rect="0,0,45,55" RadiusX="8" RadiusY="8" /...

WPF ListView : Header styling

I want to have a ListView with columns and a particular style: The background for ALL column headers should be transparent except when the mouse is over in one of them. When this happends, the ...

热门标签