【问题标题】:Styles in XAML doesn't workXAML 中的样式不起作用
【发布时间】:2013-01-09 10:48:20
【问题描述】:

为什么TabItems 的颜色总是黑色?我想要黑色的Background 和白色的字母。 Button 也应该是白色的,但它也是黑色的并且不可见。有一些冲突,但找不到在哪里。有任何想法吗?提前感谢您的帮助。

<UserControl x:Class="Test.Test"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
         xmlns:cal="http://www.caliburnproject.org"   
         xmlns:cm="clr-namespace:Caliburn.Micro;assembly=Caliburn.Micro"
         mc:Ignorable="d" d:DesignHeight="252" d:DesignWidth="894" Background="#FF111111">    

<Grid>        
    <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" VerticalAlignment="Top" Foreground="White" FontSize="48" Margin="70,-14.668,0,0" FontWeight="Light"><Run Language="de-at" Text="test test"/></TextBlock>
    <Button x:Name="Close" Content="➔" HorizontalAlignment="Left" VerticalAlignment="Top" Width="58" Foreground="White" Height="58" RenderTransformOrigin="0.5,0.5" FontSize="40" Margin="-7.625,-8,0,0" Padding="1,-5,1,1" Clip="M50.333,8 L-1.667,8 L-1.667,59.843 L50.333,59.843 z" cm:Message.Attach="Close()">
        <Button.RenderTransform>
            <TransformGroup>
                <ScaleTransform ScaleY="1" ScaleX="-1"/>
                <SkewTransform AngleY="0" AngleX="0"/>
                <RotateTransform Angle="0"/>
                <TranslateTransform/>
            </TransformGroup>
        </Button.RenderTransform>
    </Button>

    <TabControl Margin="42,52,0,0">
        <TabItem Header="Start">

        </TabItem>
            <TabItem Foreground="White" Header="Start 1" >

        </TabItem>
        <TabItem Foreground="White" Header="Start 1">

        </TabItem>
        <TabItem Foreground="White" Header="Start 1">

        </TabItem>
        <TabItem Foreground="White" Header="Start 1">

        </TabItem>
    </TabControl>       
</Grid>

我尝试了很多方法,但都没有成功。所以我所做的就是在TabItem.Header 里面放一个TextBlock:

<TabItem>
    <TabItem.Header>
        <TextBlock FontSize="25" Text="Start1" />
    </TabItem.Header>
</TabItem>

现在我可以用Foreground 更改TextBlock 的颜色。但是如果我单击TabItem,不知道如何更改TextBlock 颜色。也许我应该为此开一个新话题。感谢大家的贡献。

【问题讨论】:

  • 您希望我们如何给您答复。我们不知道所有资源字典中有什么。
  • 我删除了字典,仍然没有变化
  • 能分享一下你最新的xaml代码吗?
  • 我刚刚编辑了代码 Anand
  • “MetroCircleButtonStyle”在哪里?我刚试过这段代码,标签项看起来很正常(前景是白色的)。

标签: wpf xaml button styles tabitem


【解决方案1】:

您根本没有设置TabControl 的Background 或Foreground 属性,因此它使用的是默认颜色。

任何Control 对象的Background 属性的默认颜色为Brushes.Transparent (source),而默认Foreground 属性基于您的系统颜色(source)。

您可以在UserControl.Resources 中使用隐式样式为指定类型的所有对象设置属性,例如对所有 Control 对象使用此样式:

<UserControl.Resources>
    <Style TargetType="{x:Type Control}">
        <Setter Property="Background" Value="Black" />
        <Setter Property="Foreground" Value="White" />
    </Style>
</UserControl.Resources>

或者,如果您可以将新的画笔添加到您的 .Resources 并将其设置为 x:Key 为 SystemColors 之一的系统密钥,如下所示:

<UserControl.Resources>
    <SolidColorBrush x:Key="{x:Static SystemColors.WindowColorKey}" Color="Black"/>
    <SolidColorBrush x:Key="{x:Static SystemColors.ControlTextBrush}" Color="White"/>
</UserControl.Resources>

(您可能需要进行一些测试以找出要使用的正确 SystemColors 键。您可以找到它们的列表here)

【讨论】:

  • 我把你的代码放在我的 xaml 中(就在 之前),但仍然无法正常工作..我将稍后创建新项目并再试一次..thx
  • @Georg 查看我的更新答案。我的 SystemColors 键可能不正确。
猜你喜欢
  • 2015-04-12
  • 1970-01-01
  • 2018-02-26
  • 2021-11-20
  • 1970-01-01
  • 2019-11-23
  • 2015-11-25
  • 2014-12-22
  • 2014-06-05
相关资源
最近更新 更多