public PagingView(IPagingViewModel viewModel) { InitializeComponent(); viewModel.SetParentView(this); DataContext = viewModel; }
private static void ViewModel_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e) { IPageInfo pageInfo = d as IPageInfo; if (pageInfo == null) { return; } IPagingViewModel vm = e.OldValue as IPagingViewModel; if (vm != null) { pageInfo.UnNotifyOn("PageCount"); pageInfo.UnNotifyOn("CurrentPageRect"); } vm = e.NewValue as IPagingViewModel; if (vm != null) { pageInfo.NotifyOn("PageCount", (newValue, oldValue) => { vm.TotalPages = (int)newValue; if (vm.TotalPages > 0) { vm.RestoreSelection(); } }); pageInfo.NotifyOn("CurrentPageRect", (newValue, oldValue) => { Rect rect = (Rect)newValue; if (rect.IsEmpty) { return; } vm.PageWidth = rect.Width; vm.PageLeft = rect.Left; }); var binding = new Binding("CurrentPage") { Source = vm, Mode = BindingMode.TwoWay }; BindingOperations.SetBinding(d, VirtualizingSmartPanel.CurrentPageProperty, binding); var element = d as FrameworkElement; if (element != null) { element.Unloaded += (sender, args) => { BindingOperations.ClearBinding((DependencyObject)sender, VirtualizingSmartPanel.CurrentPageProperty); }; } } }
/// <summary> /// Sets the view model. /// </summary> /// <param name="obj">The dependency object.</param> /// <param name="value">The value.</param> public static void SetViewModel(DependencyObject obj, IPagingViewModel value) { obj.SetValue(ViewModelProperty, value); }