我有一个用户控件列表框,每个usercontrol显示有界数据的属性,并在自定义控件更改数据时未将UI更新,我的代码如下所示,将集合绑定到自定义控件:
ObservableCollection<Subscription> subscriptions = new ObservableCollection<Subscription> SubscriptionRepository.GetSubscriptions());
SubListBox.ItemsSource = subscriptions;
xaml:
<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属性发生更改并且调用它但没有更新DaysAttended时,从SubscriptionDay类调用DayChanged()。
答案 0 :(得分:0)
DaysAttended 属性还需要 setter ,对于双向绑定,您的属性必须具有getter和setter。我希望这会有所帮助。< / p>