Tôi đang cố gắng để ràng buộc hai ListBox
es:ListBox không làm mới sau khi sở hữu thay đổi
<ListBox SelectionChanged="lbApplications_SelectionChanged"
ItemsSource="{Binding Path=Applications,
UpdateSourceTrigger=PropertyChanged, Mode=OneWay}" />
<ListBox DisplayMemberPath="Message"
ItemsSource="{Binding Path=Events,
UpdateSourceTrigger=PropertyChanged, Mode=OneWay}" />
Applications
và Events
là tài sản công cộng trong Window
lớp.
tôi đặt DataContext
để this
cho cả hai hộp danh sách và thực hiện INotifyPropertyChanged
trong Window
lớp:
private void NotifyPropertyChanged(string info)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
Và rồi sau khi thêm mục mới vào Applications
hoặc Events
tôi gọi:
NotifyPropertyChanged("Events");
NotifyPropertyChanged("Applications");
Vấn đề là rằng ListBox
chỉ được tải một lần. Tôi đang làm gì sai?
Cảm ơn. Nó hoạt động, nhưng chỉ musst được đặt datacontext listbox = this hoặc ElementName = window trong {Binding}. –
Điểm tốt. Tôi đã cập nhật câu trả lời. – exclsr
Bạn không cần phải thực hiện bộ sưu tập của bạn, bạn đang ràng buộc với một thuộc tính phụ thuộc - miễn là nó là một ObservableCollection và loại trong bộ sưu tập thực hiện INotifyPropertyChanged – flobadob