【问题标题】:ObservableCollection data binding during runtime运行时的 ObservableCollection 数据绑定
【发布时间】:2014-10-31 09:35:04
【问题描述】:

我有一个小问题。我正在制作一个带有一些列表框元素的日历应用程序。每个日历视图都从 TKey = DateTimeTValue = ObservableCollection <CalendarEvent> 的字典中检索它的“日历事件”。现在,这适用于已经有预定义事件的任何日历日。我可以将列表框数据绑定到一个属性,该属性包含对该特定日历日的字典条目的引用。然而,我的应用程序的另一个特性应该是能够在运行时添加事件。我现在所做的是,如果在那个特定的日历日没有字典键,它只是将 Events 属性设置为 null,然后如果那天添加了一个事件,我会在运行时更改它,不幸的是它似乎没有喜欢这样,它不能正确地“绑定”。

这里是代码

    public CalendarDayView(DateTime date)
    {
        DataContext = this;
        Date = date;
        Events = CalendarRepository.Instance.Entries.ContainsKey(date) ? CalendarRepository.Instance.Entries[date] : null;
    }

    public DateTime Date { get; set; }
    public ObservableCollection<CalendarEvent> Events { get; set; }
    /// <summary>
    /// This method will set the listbox item source to the ObservableCollection if it hasn't been set already
    /// </summary>
    public void UpdateItemSource()
    {
        if (Events == null)
            // This is the part that doesn't do anything unfortunately
            Events = CalendarRepository.Instance.Entries[Date];
    }

XAML 标记

<ControlTemplate TargetType="{x:Type local:CalendarDayView}">
                    <Border BorderBrush="Gray" BorderThickness="0.2" Width="100" Height="100">
                        <Grid Name="contentGrid">
                            <ListBox 
                                Name="entriesListBox" Background="LightYellow" FontSize="10" 
                                     ScrollViewer.HorizontalScrollBarVisibility="Disabled"
                                ItemsSource="{Binding Events}">
                            </ListBox>
                        <!-- Date display below -->
                            <TextBlock 
                                Name="dateTextBlock" Text="{Binding Date, StringFormat={}{0:dd-MMM}, UpdateSourceTrigger=PropertyChanged}" 
                                FontFamily="Segoe UI Light" FontSize="18" VerticalAlignment="Bottom" HorizontalAlignment="Right" Margin="5"/>
                        </Grid>
                    </Border>
                </ControlTemplate>

【问题讨论】:

    标签: c# wpf data-binding calendar observablecollection


    【解决方案1】:

    我没有看到您在任何地方引发 PropertyChanged 事件来通知视图绑定更改。您应该在CalendarDayView 模型上实现INotifyPropertyChanged,并在用作绑定源的属性设置器中引发实现的PropertyChanged 事件(在本例中为Events)。

    以下代码显示了一个简单的示例,但将PropertyChanged 功能添加到基本模型类可能会更好。

    public class CalendarDayView : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
    
        private ObservableCollection<CalendarEvent> _events;
    
        public ObservableCollection<CalendarEvent> Events 
        { 
            get { return _events; }
            set
            {
                _events = value;
                RaisePropertyChanged("Events");
            }
        }
    
        protected void RaisePropertyChanged(string propertyName)
        {
            var handler = PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    

    【讨论】:

    • 谢谢,这正是我要找的!
    猜你喜欢
    • 1970-01-01
    • 2011-12-23
    • 2016-11-28
    • 2011-06-16
    • 2017-11-21
    • 1970-01-01
    • 2010-11-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多