【问题标题】:How do you change the foreground color of a stackpanel in xaml如何在 xaml 中更改堆栈面板的前景色
【发布时间】:2014-03-16 22:38:35
【问题描述】:

我的 App.xaml 中有这个

 <Style x:Key="ColorBlack" TargetType="StackPanel">
      <Setter Property="TextElement.Foreground" Value="Black"></Setter>
 </Style>

这在 xaml 页面上

 <StackPanel Style="{StaticResource ColorBlack}"></StackPanel>

在设计器中颜色变为黑色,但是当我在设备上运行应用程序时它崩溃并说它不知道名称 ColorBlack。

【问题讨论】:

标签: xaml


【解决方案1】:

你可以试试这样的:

<StackPanel>
    <StackPanel.Resources>
        <Style TargetType="{x:Type FrameworkElement}">
            <Setter Property="TextBlock.Foreground" Value="Blue" />
        </Style>
        <Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource {x:Type TextBlock}}" />
        <Style TargetType="{x:Type Label}" BasedOn="{StaticResource {x:Type Label}}" />
        <Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type FrameworkElement}}" />
        <Style TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type TextBox}}" />

    </StackPanel.Resources>
    <TextBlock Text="TextBlock"/>
    <Label Content="Label"/>
    <Button Content="Button"/>
    <TextBox Text="TextBox"/>
</StackPanel>

【讨论】:

  • 对于应用到元素的隐式样式,TargetType 必须完全匹配。它不适用于像 FrameworkElement 这样的基类。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-08
  • 1970-01-01
  • 2011-12-24
  • 1970-01-01
  • 1970-01-01
  • 2017-09-15
相关资源
最近更新 更多