简体   繁体   English

为什么这种简单的样式不适用?

[英]Why this simple style doesn't apply?

Why TextBlock remains black? 为什么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>

Update: Ok now I have another problem. 更新:好的,现在我有另一个问题。 The style does not react on property change: 样式对属性更改没有反应:

<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>

Backing Code: 备用代码:

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's text changes but color does not TextBlock的文本更改,但颜色不变

Change it to Property Trigger 将其更改为属性触发器

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

TemplatedParent works inside a ControlTemplate. TemplatedParent在ControlTemplate内部工作。 Your Binding is incorrect. 您的绑定不正确。 Thats why it doesn't work. 这就是为什么它不起作用。

If you want to use DataTrigger for some reason then the correct Binding would be 如果出于某种原因要使用DataTrigger,则正确的Binding将是

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

只需更改绑定:

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

This primarily is happening because Tag property is of type object and not string. 这主要是因为Tag属性是object类型而不是string类型。 The solution given in below link might help you: 以下链接中给出的解决方案可能会帮助您:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM