DataTrigger不会更改Text属性 [英] DataTrigger does not change Text property

查看:129
本文介绍了DataTrigger不会更改Text属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对样式使用数据触发来更改属性。

I'm attempting to use a data-trigger on a style to change a property.

符合 最小,完整和可验证的示例的要求...

要重现,请首先在Visual Studio中创建一个WPF应用程序。

To reproduce, first create a WPF application in Visual Studio.

在App.xaml中.cs:

Within the App.xaml.cs :

using System.ComponentModel;
using System.Windows;

namespace Foo{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application, INotifyPropertyChanged {
        private bool _clicked;
        public bool Clicked {
            get { return this._clicked; }
            set {
                this._clicked = value;
                this.PropertyChanged?.Invoke(
                    this, new PropertyChangedEventArgs( "Clicked" ) );
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
    }
}

在MainWindow.xaml中:

Within the MainWindow.xaml :

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:lib="clr-namespace:System;assembly=mscorlib"
    xmlns:local="clr-namespace:Foo"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    mc:Ignorable="d" x:Class="Foo.MainWindow"
    Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <lib:Boolean x:Key="True">True</lib:Boolean>
    </Window.Resources>
    <Grid>
        <Button x:Name="button" Click="button_Click">
            <Viewbox>
                <TextBlock Text="Unclicked">
                    <TextBlock.Style>
                        <Style TargetType="{x:Type TextBlock}">
                            <Style.Triggers>
                                <DataTrigger
                                        Binding="{Binding
                                            Clicked,
                                            Source={x:Static Application.Current}}"
                                            Value="{StaticResource True}">
                                    <Setter Property="Text" Value="Clicked" />
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </TextBlock.Style>
                </TextBlock>
            </Viewbox>
        </Button>
    </Grid>
</Window>

在MainWindow.xaml.cs中-

Within the MainWindow.xaml.cs -

using System.Windows;

namespace Foo{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window {
        public MainWindow( ) {
            InitializeComponent( );
        }

        private void button_Click( object sender, RoutedEventArgs e ) {
            ( Application.Current as App ).Clicked = !( Application.Current as App ).Clicked;
        }
    }
}

作为旁注-我尝试将数据触发器的值设置为 True ,但这也不起作用(触发器未捕获,并且基于设置该属性的文本也未更改到一个新值)。

As a side note - I tried setting the value of the data trigger to just "True", and that also did not work ( the trigger did not catch, and text did not change based on setting the property to a new value ).

那么为什么数据触发在这里没有捕获或起作用? (使用静态资源还是使用文字值)?更相关-为什么会出现此错误? 正在使用(密封) DataTrigger后,无法对其进行修改错误?什么是完成我要在这里完成的正确方法? (最好还是使用数据触发而不是转换器,因为我确实需要在两个值之间切换)。

So why is the data-trigger not catching or working here? ( Either with the static resource or the literal value )? Even more relevant - why am I getting this error? The "After a 'DataTrigger' is in use (sealed), it cannot be modified" error? And what is the proper method of accomplishing what I am trying to do here? ( Preferably still using a data-trigger and not a converter, since I do need to switch between two values ).

推荐答案

分配给TextBlock的Text属性的本地值的优先级高于DataTrigger中Setter提供的值。有关详细信息,请参见依赖项属性值优先级

The local value assigned to the TextBlock's Text property has higher precedence than the value provided by the Setter in the DataTrigger. See Dependency Property Value Precedence for details.

由另一个设置器设置初始文本值:

Set the initial Text value by another Setter:

<TextBlock>
    <TextBlock.Style>
        <Style TargetType="TextBlock">
            <Setter Property="Text" Value="Unclicked"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding Clicked,
                                       Source={x:Static Application.Current}}"
                             Value="{StaticResource True}">
                    <Setter Property="Text" Value="Clicked" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBlock.Style>
</TextBlock>

使用Boolean资源时看到的错误消息只是XAML设计人员抱怨。在运行时没有错误。

The error message you see when you use the Boolean resource is just the XAML designer complaining. There is no error at runtime.

这篇关于DataTrigger不会更改Text属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆