| 1 | using System; |
| 2 | using System.Collections.Generic; |
| 3 | using System.Diagnostics; |
| 4 | using System.Linq; |
| 5 | using System.Text; |
| 6 | using System.Threading.Tasks; |
| 7 | using System.Windows; |
| 8 | using System.Windows.Controls; |
| 9 | using System.Windows.Media; |
| 10 | |
| 11 | namespace UI.Controls |
| 12 | { |
| 13 | public class TPage : Page |
| 14 | { |
| 15 | ///<summary> |
| 16 | /// |
| 17 | ///</summary> |
| 18 | public bool IsFillPage |
| 19 | { |
| 20 | get { return (bool)GetValue(IsFillPageDependencyProperty); } |
| 21 | set { SetValue(IsFillPageDependencyProperty, value); } |
| 22 | } |
| 23 | public static DependencyProperty IsFillPageDependencyProperty = DependencyProperty.Register("IsFillPage", typeof(bool), typeof(TPage)); |
| 24 | |
| 25 | private PageContainer pageContainer; |
| 26 | public TPage() |
| 27 | { |
| 28 | DefaultStyleKey = typeof(TPage); |
| 29 | Loaded += TPage_Loaded; |
| 30 | } |
| 31 | |
| 32 | private void TPage_Loaded(object sender, RoutedEventArgs e) |
| 33 | { |
| 34 | UpdatePageSize(); |
| 35 | } |
| 36 | |
| 37 | public override void OnApplyTemplate() |
| 38 | { |
| 39 | base.OnApplyTemplate(); |
| 40 | HandleEvent(); |
| 41 | } |
| 42 | |
| 43 | private void HandleEvent() |
| 44 | { |
| 45 | if (pageContainer != null) |
| 46 | { |
| 47 | pageContainer.SizeChanged -= Pc_SizeChanged; |
| 48 | } |
| 49 | |
| 50 | if (IsFillPage) |
| 51 | { |
| 52 | |
| 53 | var parent = VisualTreeHelper.GetParent(this); |
| 54 | if (parent != null) |
| 55 | { |
| 56 | while (!(parent is PageContainer)) |
| 57 | { |
| 58 | parent = VisualTreeHelper.GetParent(parent); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | pageContainer = parent as PageContainer; |
| 63 | if (pageContainer != null) |
| 64 | { |
| 65 | pageContainer.SizeChanged += Pc_SizeChanged; |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | private void Pc_SizeChanged(object sender, SizeChangedEventArgs e) |
| 71 | { |
| 72 | UpdatePageSize(); |
| 73 | } |
| 74 | |
| 75 | private void UpdatePageSize() |
| 76 | { |
| 77 | Width = pageContainer.ActualWidth; |
| 78 | Height = pageContainer.ActualHeight; |
| 79 | |
| 80 | Debug.WriteLine("UpdatePageSize"); |
| 81 | } |
| 82 | } |
| 83 | } |