c# - DataTrigger 不会更改 Text 属性

标签 c# wpf xaml datatrigger

我试图在样式上使用数据触发器来更改属性。

符合“Minimal, Complete and Verifiable Example”要求...

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

在 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 中:

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

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",但这也没有用(触发器没有捕获,并且文本没有根据将属性设置为新值)。

那么为什么数据触发器在这里没有捕获或工作? (使用静态资源或文字值)?更相关 - 为什么我会收到此错误? “在使用(密封)'DataTrigger' 之后,无法修改它”错误?完成我在这里要做的事情的正确方法是什么? (最好仍然使用数据触发器而不是转换器,因为我确实需要在两个值之间切换)。

最佳答案

分配给 TextBlock 的 Text 属性的本地值 比 DataTrigger 中的 Setter 提供的值具有更高的优先级。参见 Dependency Property Value Precedence了解详情。

通过另一个 Setter 设置初始 Text 值:

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

您在使用 bool 资源时看到的错误消息只是 XAML 设计器的提示。运行时没有错误。

关于c# - DataTrigger 不会更改 Text 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44381133/

相关文章:

WPF - 如何制作自定义边框

.net - 将静态方法绑定(bind)到 CheckBox 并将参数传递给它

c# - 更改 XAML 中的绑定(bind)值

c# - 为什么我的 SqlCommand 返回一个字符串,而它应该是一个 int?

c# - 如何将 WEP key 转换为 ASCII 密码?

c# - 如何在 C# 中访问 Outlook 电子邮件附件而不将其保存到新位置?

c# - 在数据库asp.net mvc中保存动态创建的控件数据

c# - 从绑定(bind)的用户控件访问 Datacontext

xaml - Appbar 按钮图标图像在 Window 8.1 中似乎不正确

WPF 数据网格行水平对齐问题