WPF Button DataTrigger 在属性更改时不更新内容

问题描述 投票:0回答:1

我正在为学校项目开发 WPF 电影数据库应用程序,我有一个按钮,我想根据

IsFavorite
对象的 bool 属性
SelectedFilm
来切换按钮的内容。当
IsFavorite
true
时,按钮应显示“从收藏夹中删除”。当
IsFavorite
false
时,按钮应显示“添加到收藏夹”。

我放置了

textblock
只是为了测试并查看属性和绑定是否有效,并且它确实工作正常(如果我单击按钮,它会从 true 变为 false)

尽管属性正确更改,但

Button
不会更新其内容。

如有任何帮助,我们将不胜感激。

MainWindow.xaml

<Window x:Class="MovieDatabase.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:viewModels="clr-namespace:MovieDatabase"
        Title="MovieDatabase" Height="600" Width="1100" Background="#0f1013" Foreground="white" FontFamily="Arial">

    <Window.DataContext>
       <viewModels:ViewModel/>
    </Window.DataContext>

    <!-- Button with DataTrigger -->
    <StackPanel Orientation="Horizontal" Margin="0,10" DataContext="{Binding SelectedFilm}">
        <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 IsFavorite}" Value="True">
                            <Setter Property="Content" Value="Remove from Favorites" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </Button.Style>
        </Button>
    </StackPanel>

    <!-- TextBlock to test IsFavorite binding -->
    <TextBlock Text="{Binding SelectedFilm.IsFavorite}" Foreground="White" Margin="10,0,0,0" Background="red" Width="30" HorizontalAlignment="Left"/>
</Window>

MainWindow.xaml.cs

public MainWindow()
{
    InitializeComponent();
    DataContext = new ViewModel();
}

private void ToggleFavorite_Click(object sender, RoutedEventArgs e)
{
    if (DataContext is ViewModel viewModel && viewModel.SelectedFilm != null)
    {
        viewModel.SelectedFilm.IsFavorite = !viewModel.SelectedFilm.IsFavorite;
    }
}

ViewModel.cs

public class ViewModel : INotifyPropertyChanged
{
    private Film _selectedFilm;
    public Film SelectedFilm
    {
        get => _selectedFilm;
        set
        {
            _selectedFilm = value;
            OnPropertyChanged(nameof(SelectedFilm));
        }
    }

    public ICommand ToggleFavoriteCommand { get; }

    public ViewModel()
    {
        ToggleFavoriteCommand = new RelayCommand(ToggleFavorite, () => SelectedFilm != null);
    }

    private void ToggleFavorite()
{
    if (SelectedFilm != null)
    {
        SelectedFilm.IsFavorite = !SelectedFilm.IsFavorite;

        OnPropertyChanged(nameof(SelectedFilm));
        OnPropertyChanged(nameof(SelectedFilm.IsFavorite));

        if (SelectedGenre == "Favorites")
        {
            FilteredFilms.Refresh();
        }
    }
}

    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string propertyName) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

电影.cs

public class Film : INotifyPropertyChanged
{
    private bool _isFavorite;
    public bool IsFavorite
    {
        get => _isFavorite;
        set
        {
            if (_isFavorite != value)
            {
                _isFavorite = value;
                OnPropertyChanged(nameof(IsFavorite));
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string propertyName) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

c# wpf binding datatrigger
1个回答
0
投票

这不是答案,而是澄清问题根源的建议。 我无法将其作为评论给出,因为有代码。 看完你的评论,我会删除这个答案。

我在您的代码中没有看到问题的根源。 因此,我只能假设 TextBox 和 Button 的示例在某种程度上有所不同,但您没有注意到它。

尝试运行此代码,并写出你得到的结果:

    <!-- Button with DataTrigger -->
    <StackPanel Orientation="Horizontal" Margin="0,10">
    <TextBlock Text="{Binding SelectedFilm.IsFavorite}" Foreground="White" Margin="10,0,0,0" Background="red" Width="30" HorizontalAlignment="Left"/>
    <Button DataContext="{Binding SelectedFilm}" Content="{Binding IsFavorite}" Foreground="White" Margin="10,0,0,0" Background="red" Width="30" HorizontalAlignment="Left"/>
    <Button Content="{Binding IsFavorite}" Foreground="White" Margin="10,0,0,0" Background="red" Width="30" HorizontalAlignment="Left"/>
        <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>
© www.soinside.com 2019 - 2024. All rights reserved.