当 ObservableCollection 更改时,ItemsControl 不会更新

ItemsControl does not update when ObservableCollection changes

我正在创建日历并使用 MVVM light。当我更改可观察集合时,UI 不会更新。 我的目标是,如果我单击 ItemsControl 中的按钮,可观察到的 Collection 会发生变化,并且应该在我的按钮样式中触发 DataTriger。

主窗口中的内容:

private ObservableCollection<Dates> _list;
public ObservableCollection<Dates> List
        {
            get { return _list; }
            set
            {
                _list = value;
                RaisePropertyChanged(nameof(List));
            }
        }

public RelayCommand<DateTime> DayClickedCommand { get; private set; }
private void DayClickedeCommandParameterhandling(DateTime s)
        {
            for (int i = 0; i < List.Count; i++)
            {
                if (List[i].Date == s)
                {
                    List[i].IsSelected = true;
                }
            }
        }

日期 class:

public class Dates
    {
        public Dates(DateTime day, int currentMonth)
        {
            Date = day;
            Day = day.Day.ToString();
            if ((int)day.Month == currentMonth)
            {
                IsInactive = false;
            }
            else
            {
                IsInactive = true;
            }
            if (day.DayOfYear == DateTime.Today.DayOfYear)
            {
                IsToday = true;
            }
            else IsToday = false;
        }

        #region Properties
        public DateTime Date { get; set; }
        public string Day { get; set; }
        public bool IsInactive { get; set; }
        public bool IsToday { get; set; }
        public bool IsSelected { get; set; }


        #endregion Properties
    }

我的Xaml:

 <ItemsControl ItemsSource="{Binding List}">
                        <ItemsControl.ItemsPanel>
                            <ItemsPanelTemplate>
                                <WrapPanel></WrapPanel>
                            </ItemsPanelTemplate>
                        </ItemsControl.ItemsPanel>
                        <ItemsControl.ItemTemplate>
                            <DataTemplate >
                                <Grid Height="{Binding ElementName=Monday, Path=ActualHeight}" Width="{Binding ElementName=Monday, Path=ActualWidth}" Name="grid">
                                    <Button  Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=DataContext.DayClickedCommand}" CommandParameter="{Binding Date}"  VerticalContentAlignment="Center" HorizontalContentAlignment="Center" MinHeight="10" MinWidth="10" FontSize="{Binding ElementName=textblockMonday, Path=FontSize}" Name="Button" BorderThickness="3" Content="{Binding Day}" >
                                            <Button.Style>
                                            <Style TargetType="Button">
                                                <Style.Triggers>
                                                    <DataTrigger Binding="{Binding IsInactive}" Value="True">
                                                        <Setter Property="Foreground" Value="Green" />
                                                        <Setter Property="Background" Value="Transparent" />
                                                    </DataTrigger> 
                                                    <DataTrigger Binding="{Binding IsInactive}" Value="false">
                                                        <Setter Property="Background" Value="Transparent" />
                                                    </DataTrigger>
                                                    <DataTrigger Binding="{Binding IsToday}" Value="true">
                                                        <Setter Property="Background" Value="Yellow" />
                                                        <Setter Property="Foreground" Value="Orange" />
                                                        <Setter Property="BorderBrush" Value="Orange" />
                                                    </DataTrigger> 
                                                    <DataTrigger Binding="{Binding IsToday}" Value="false">
                                                        <Setter Property="BorderBrush" Value="Transparent" />
                                                    </DataTrigger>
                                                    <DataTrigger Binding="{Binding IsSelected}" Value="true">
                                                        <Setter Property="Background" Value="Green" />
                                                        <Setter Property="Foreground" Value="Purple" />
                                                        <Setter Property="BorderBrush" Value="Orange" />
                                                    </DataTrigger>
                                                </Style.Triggers>
                                                </Style>
                                        </Button.Style>
                                    </Button>
                                </Grid>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ItemsControl>

命令正在运行,集合已更改,但未通知 ui。

您的 Dates class 应该实施 INotifyPropertyChanged 并为 IsSelected 引发 PropertyChanged 事件 属性:

public class Dates : INotifyPropertyChanged
{
    public Dates(DateTime day, int currentMonth)
    {
        Date = day;
        Day = day.Day.ToString();
        if ((int)day.Month == currentMonth)
        {
            IsInactive = false;
        }
        else
        {
            IsInactive = true;
        }
        if (day.DayOfYear == DateTime.Today.DayOfYear)
        {
            IsToday = true;
        }
        else IsToday = false;
    }

    #region Properties
    public DateTime Date { get; set; }
    public string Day { get; set; }
    public bool IsInactive { get; set; }
    public bool IsToday { get; set; }

    private bool _isSelected;
    public bool IsSelected
    {
        get { return _isSelected; }
        set { _isSelected = value; NotifyPropertyChanged(); }
    }
    #endregion Properties

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged([System.Runtime.CompilerServices.CallerMemberName]
        string propertyName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

ObservableCollection<T> 中的 T 项被修改时不会引发更改通知。仅当您向其中添加项目或从中删除项目时,它才会引发事件。