You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WPF Button的DataTrigger在属性变更时未更新内容问题

问题原因及修复方案

核心问题1:重复创建ViewModel实例

你的MainWindow在XAML和代码后置中分别初始化了两个独立的ViewModel对象:

  • XAML通过<Window.DataContext>创建了一个实例
  • 构造函数里又执行DataContext = new ViewModel();生成另一个

这会导致XAML绑定的ViewModel与代码中操作的ViewModel不是同一个,虽然TextBlock的绑定路径巧合能正常显示,但按钮的触发器绑定会因实例不匹配无法响应变化。

修复步骤

删除MainWindow.xaml.cs构造函数中的DataContext = new ViewModel();语句,保留XAML中的DataContext设置即可:

public MainWindow()
{
    InitializeComponent();
    // 移除该行:DataContext = new ViewModel();
}

核心问题2:Style触发器的绑定响应问题

尽管Film类正确实现了INotifyPropertyChanged,但Style的DataTrigger在嵌套属性变化时可能存在响应延迟。提供两种修复方案:

方案A:明确绑定完整路径

移除StackPanel的DataContext="{Binding SelectedFilm}"设置,直接在按钮的DataTrigger中绑定完整路径:

<StackPanel Orientation="Horizontal" Margin="0,10">
    <Button x:Name="ToggleFavoriteButton" Background="Red" BorderThickness="0" Click="ToggleFavorite_Click" Foreground="White" Padding="5">
        <Button.Style>
            <Style TargetType="Button">
                <Setter Property="Content" Value="Add to Favorites" />
                <Style.Triggers>
                    <DataTrigger Binding="{Binding SelectedFilm.IsFavorite}" Value="True">
                        <Setter Property="Content" Value="Remove from Favorites" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Button.Style>
    </Button>
</StackPanel>

方案B:使用转换器实现文本切换(更灵活)

  1. 创建转换器类:
public class FavoriteTextConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return (bool)value ? "Remove from Favorites" : "Add to Favorites";
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
  1. 在XAML中引用并绑定按钮Content:
<Window.Resources>
    <viewModels:FavoriteTextConverter x:Key="FavoriteTextConverter"/>
</Window.Resources>

<StackPanel Orientation="Horizontal" Margin="0,10">
    <Button x:Name="ToggleFavoriteButton" 
            Background="Red" 
            BorderThickness="0" 
            Click="ToggleFavorite_Click" 
            Foreground="White" 
            Padding="5"
            Content="{Binding SelectedFilm.IsFavorite, Converter={StaticResource FavoriteTextConverter}}"/>
</StackPanel>

优化建议:采用MVVM规范的Command绑定

你的ViewModel已定义ToggleFavoriteCommand,建议直接绑定到按钮的Command属性,移除代码后置的Click事件,让逻辑集中在ViewModel中:

<Button x:Name="ToggleFavoriteButton" 
        Background="Red" 
        BorderThickness="0" 
        Foreground="White" 
        Padding="5"
        Command="{Binding ToggleFavoriteCommand}"
        Content="{Binding SelectedFilm.IsFavorite, Converter={StaticResource FavoriteTextConverter}}"/>

同时删除MainWindow.xaml.cs中的ToggleFavorite_Click方法。


内容的提问来源于stack exchange,提问作者Ela Škalič

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 06:44:55