WPF DataTrigger为false,但不会恢复为先前的值

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

我最近创建了一个用户控件,该控件通知用户他有多少个通知。由于我希望此用户控件仅在有任何通知时才显示自身,因此我创建了一个DataTrigger,该数据绑定到bool并将可见性设置为在没有通知时折叠。它在一开始就可以正常工作,并且用户控件已折叠,但是在我的程序中,我添加了一个通知并在bool上调用NotifyPropertyChanged,但是由于某些未知原因,用户控件仍然保持隐藏状态。

用户控制

<UserControl.Style>
    <Style TargetType="{x:Type UserControl}">
        <Setter Property="Visibility"
                    Value="Visible">
        </Setter>
        <Style.Triggers>
            <DataTrigger Binding="{Binding HasNotifications}"
                             Value="False">
                <Setter Property="Visibility"
                            Value="Collapsed">
                </Setter>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</UserControl.Style>

DataContext

private ObservableCollection<Notification> notifications;
public ObservableCollection<Notification> Notifications
{
    get { return notifications; }
}

public bool HasNotifications
{
    get
    {
        return Notifications.Count > 0;
    }
}

public void AddNotification(Notification notificationToAdd)
{
    notifications.Add(notificationToAdd);
    NotifyPropertyChanged(nameof(Notifications));
    NotifyPropertyChanged(nameof(HasNotifications));
}

public event PropertyChangedEventHandler PropertyChanged;

protected virtual void NotifyPropertyChanged(string propertyName)
{
    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

[我还试图将DataTrigger的值设置为相反的值(当HasNotifications为true时将可见性设置为on,但对我来说也没有用。

c# wpf xaml datatrigger
1个回答
2
投票

我复制了您的代码并在3.5和4.7框架下对其进行了测试,并且可以正常工作。我只能在一种情况下重现您的问题,但我想在这里是因为该部分不在您的代码示例中。

仅当数据上下文类不继承自INotifyPropertyChanged时,它才起作用。否则,最少的代码示例将按预期工作。

public class MyDataContext :  INotifyPropertyChanged // <= doesn't work if this is missing
{
    private ObservableCollection<Notification> notifications = new ObservableCollection<Notification>();
    public ObservableCollection<Notification> Notifications => notifications;

    public bool HasNotifications=> Notifications.Count > 0;

    public void AddNotification(Notification notificationToAdd)
    {
        notifications.Add(notificationToAdd);
        NotifyPropertyChanged(nameof(Notifications));
        NotifyPropertyChanged(nameof(HasNotifications));
    }

    private ICommand _AddNotification;
    public ICommand AddNotificationCMD => _AddNotification ?? (_AddNotification = new RelayCommand<object>(a => AddNotificationCommand(a)));

    private void AddNotificationCommand(object item)
    {
        AddNotification(new Notification());
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void NotifyPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

<Window x:Class="WpfApp_3_5_framework_test.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfApp_3_5_framework_test"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800">
<Grid>
    <local:UserControl1/>
    <Button Content="Add Notification" Command="{Binding AddNotificationCMD}" Height="20" Width="200" />
</Grid>
</Window>

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = new MyDataContext();
    }
}

<UserControl x:Class="WpfApp_3_5_framework_test.UserControl1"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:WpfApp_3_5_framework_test"
         mc:Ignorable="d" 
         d:DesignHeight="450" d:DesignWidth="800">
<UserControl.Style>
    <Style TargetType="{x:Type UserControl}">
        <Setter Property="Visibility"
                Value="Visible">
        </Setter>
        <Style.Triggers>
            <DataTrigger Binding="{Binding HasNotifications}"
                         Value="False">
                <Setter Property="Visibility"
                        Value="Collapsed">
                </Setter>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</UserControl.Style>
<Grid>
    <StackPanel>
        <Border Width="100" Height="100" Background="Red"/>
    </StackPanel>
</Grid>
</UserControl>
© www.soinside.com 2019 - 2024. All rights reserved.