0

我有一个用户控件列表框,每个用户控件显示有界数据的属性,并将一个集合绑定到自定义控件,当自定义控件更改数据时 UI 未更新我的代码如下:

ObservableCollection<Subscription> subscriptions = new ObservableCollection<Subscription> SubscriptionRepository.GetSubscriptions());
            SubListBox.ItemsSource = subscriptions;

xml:

<DataTemplate x:Key="MyDataTemplate">
            <UserControls:SubscriptionUC />
</DataTemplate>

<ListBox x:Name="SubListBox" ItemTemplate="{StaticResource MyDataTemplate}">
</ListBox>

用户控制:

    <TextBlock Text="{Binding Path=DaysAttended}" />
<cc:CustomControl SubscriptionSource="{Binding Path=SubscriptionDays,Mode=TwoWay}" />

订阅类:

public class Subscription : INotifyPropertyChanged
    {
        public int SubscriptionTypeId { get; set; }

        public DateTime StartDate { get; set; }

        public DateTime EndDate { get; set; }

        public ObservableCollection<SubscriptionDay> SubscriptionDays { get; set; }

        public int DaysAttended { get { return SubscriptionDays.Count(d => d.Attended == true); } }
public void DayChanged()
        {
            RaisePropertyChanged("SubscriptionDays");
            RaisePropertyChanged("DaysAttended");
        }
 }

当 SubscriptionDay 属性发生变化时,从 SubscriptionDay 类调用 DayChanged() 并调用它,但 DaysAttended 未更新。

4

1 回答 1

0

您还需要为DaysAttended属性设置 setter,对于双向绑定,您的属性必须具有 getter 和 setter。我希望这会有所帮助。

于 2013-02-03T13:51:51.277 回答