【问题标题】:Why this simple style doesn't apply?为什么这种简单的风格不适用?
【发布时间】:2011-10-08 15:58:13
【问题描述】:

为什么 TextBlock 仍然是黑色的?

<Window x:Class="WpfApplication4.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <Style TargetType="TextBlock" x:Key="style">
            <Style.Triggers>
                <DataTrigger Binding="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Tag}" Value="True">
                    <Setter Property="Foreground" Value="Red" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <StackPanel>
        <TextBlock Text="Test" Tag="True" Style="{StaticResource style}" />
    </StackPanel>
</Window>

更新:好的,现在我有另一个问题。样式不会对属性更改做出反应:

<Window x:Class="WpfApplication4.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <Style TargetType="TextBlock" x:Key="style">
            <Style.Triggers>
                <Trigger Property="Tag" Value="True">
                    <Setter Property="Foreground" Value="Red" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <StackPanel>
        <TextBlock Text="{Binding Prop}" Tag="{Binding Prop}" Style="{StaticResource style}" x:Name="text" />
        <Button Content="Test" Click="Button_Click" />
    </StackPanel>
</Window>

支持代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.ComponentModel;

namespace WpfApplication4
{
    public partial class MainWindow : Window
    {
        private MyClass a = new MyClass();

        public MainWindow()
        {
            InitializeComponent();
            DataContext = a;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            a.Prop = true;
            a.OnPropertyChanged("Prop");
        }
    }

    public class MyClass : INotifyPropertyChanged
    {
        public bool Prop { get; set; }

        public event PropertyChangedEventHandler PropertyChanged;
        public virtual void OnPropertyChanged(string property)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    }
}

TextBlock 的文字改变但颜色不变

【问题讨论】:

  • 请不要接受答案,然后用另一个问题更新问题。而是提出一个新问题。

标签: c# .net wpf xaml styles


【解决方案1】:

改为属性触发器

    <Style TargetType="TextBlock" x:Key="style">
        <Style.Triggers>
            <Trigger Property="Tag" Value="True">
                <Setter Property="Foreground" Value="Red" />
            </Trigger>
        </Style.Triggers>
    </Style>

TemplatedParent 在 ControlTemplate 中工作。您的绑定不正确。这就是为什么它不起作用。

如果您出于某种原因想要使用 DataTrigger,那么正确的 Binding 应该是

<DataTrigger Binding="{Binding Tag,RelativeSource={RelativeSource Self}}" Value="True">

【讨论】:

    【解决方案2】:

    只需更改绑定:

    Binding="{Binding RelativeSource={RelativeSource Self}, Path=Tag}"
    

    【讨论】:

    • 我刚刚发现它适用于您建议的绑定,但不适用于Trigger。看起来它试图比较布尔值和字符串并且总是返回 false。
    【解决方案3】:

    这主要是因为 Tag 属性是对象类型而不是字符串。以下链接中给出的解决方案可能会对您有所帮助:

    http://social.msdn.microsoft.com/forums/en-US/wpf/thread/d3424267-ed1f-4b30-90a1-5cca9843bd22

    关于 Textblock.Tag 属性: http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.tag.aspx

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-10
      • 2011-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多