/// <summary>垂直方向時のExpanderの内部処理拡張メソッド</summary> /// <param name="expander"></param> /// <param name="mode"></param> private static void AttachMode_Vertical(this Expander expander, GridSnapMode mode) { if (expander == null) { return; } // イベント解除 expander.Expanded -= Expanded_Vertical; expander.Collapsed -= Collapsed_Vertical; Grid targetGrid = expander.FindAncestor <Grid>(); if (targetGrid == null) { return; } // 対象となるGridの情報を取得していく int gridRow = Grid.GetRow(expander); RowDefinition targetRowDefinition = targetGrid.RowDefinitions[gridRow]; // プロパティ内に情報をセット SetTargetRowDefinition(expander, targetRowDefinition); SetLastGridLength(expander, targetRowDefinition.Height); // GridSplitterを取得 GridSplitter gridSplitter = null; if (mode == GridSnapMode.Auto) { gridSplitter = targetGrid.FindDescendant <GridSplitter>(); } else { gridSplitter = GetTargetGridSplitter(expander); } if (mode == GridSnapMode.Explicit && gridSplitter == null) { // GridSplitterの移動をそのままにするのにGridSplitterがnullはあり得ない throw new ArgumentException("Mode [explicit] requires [TargetGridSplitter]"); } // Binding実行 gridSplitter.SetBinding(UIElement.VisibilityProperty, new Binding(nameof(Expander.IsExpanded)) { Mode = BindingMode.OneWay, Converter = BoolToVisibleConverter, Source = expander }); expander.Expanded += Expanded_Vertical; expander.Collapsed += Collapsed_Vertical; }
private static void AddGridSplitterBindings(GridSplitter gridSplitter, DisplayContainer previousContainer, DisplayContainer nextContainer) { MultiBinding multiBinding = new MultiBinding() { Converter = new BooleanAndConverter() }; // A splitter is disabled if either the container above or below it has its content collapsed var booleanToNotBooleanConverter = new BooleanToNotBooleanConverter(); multiBinding.Bindings.Add(new Binding(nameof(previousContainer.IsContentCollapsed)) { Source = previousContainer, Converter = booleanToNotBooleanConverter }); multiBinding.Bindings.Add(new Binding(nameof(nextContainer.IsContentCollapsed)) { Source = nextContainer, Converter = booleanToNotBooleanConverter }); gridSplitter.SetBinding(GridSplitter.IsEnabledProperty, multiBinding); }
private static void AttachMode_Horizontal(this Expander expander, GridSnapMode mode) { if (expander == null) { return; } expander.Expanded -= Expanded_Horizontal; expander.Collapsed -= Collapsed_Horizontal; Grid targetGrid = expander.FindAncestor <Grid>(); if (targetGrid == null) { return; } if (mode == GridSnapMode.None) { return; } int gridColumn = Grid.GetColumn(expander); ColumnDefinition columnDefinition = targetGrid.ColumnDefinitions[gridColumn]; SetTargetColumnDefinition(expander, columnDefinition); // 初回にExpanderを開く際、開かれたGridのActualWidthをとれずに検索テキストボックスの幅が微小になってしまう // そのため、exeを立ち上げて最初にExpanderを開く際のみMainWindowの比で強引に値を入れてやる // Expanderを開く前はExpander側の幅を調節できないため、数値がおかしくなる心配はないと想定される if (_isFirst) { SetFirstGridLength(expander); } else { SetLastGridLength(expander, columnDefinition.Width); } GridSplitter gridSplitter = null; if (mode == GridSnapMode.Auto) { gridSplitter = targetGrid.FindDescendant <GridSplitter>(); } else { gridSplitter = GetTargetGridSplitter(expander); } if (mode == GridSnapMode.Explicit && gridSplitter == null) { throw new ArgumentException("[Explicit] requires [TargetGridSplitter]"); } gridSplitter.SetBinding(UIElement.VisibilityProperty, new Binding(nameof(Expander.IsExpanded)) { Mode = BindingMode.OneWay, Converter = BoolToVisibleConverter, Source = expander, }); expander.Expanded += Expanded_Horizontal; expander.Collapsed += Collapsed_Horizontal; }
//根据行数列数绘制表格 void DrawGrid() { ColumnDefinition columnDefinition; RowDefinition rowDefinition; GridSplitter gridSplitter; Binding binding; if (RowCount == 0 || ColumnCount == 0) { return; } base.ColumnDefinitions.Clear(); base.RowDefinitions.Clear(); base.Children.Clear(); for (int i = 0; i < RowCount; i++) { rowDefinition = new RowDefinition() { AllowDrop = true }; base.RowDefinitions.Add(rowDefinition); if (i < RowCount - 1) { base.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(0, GridUnitType.Auto) }); } } for (int i = 0; i < ColumnCount; i++) { columnDefinition = new ColumnDefinition() { AllowDrop = true }; base.ColumnDefinitions.Add(columnDefinition); if (i < ColumnCount - 1) { base.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(0, GridUnitType.Auto) }); } } for (int row = 0; row < base.RowDefinitions.Count; row += 2) { for (int column = 0; column < base.ColumnDefinitions.Count; column += 2) { base.Children.Add(GetTextBox(column, row)); } } for (int column = 1; column < base.ColumnDefinitions.Count; column += 2) { gridSplitter = new GridSplitter() { AllowDrop = false, Background = Brushes.Black, ShowsPreview = true, HorizontalAlignment = System.Windows.HorizontalAlignment.Center }; binding = new Binding() { Source = this, Path = new PropertyPath("BorderWidth"), Mode = BindingMode.TwoWay, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged }; gridSplitter.SetBinding(GridSplitter.WidthProperty, binding); Grid.SetColumn(gridSplitter, column); Grid.SetRowSpan(gridSplitter, base.RowDefinitions.Count); base.Children.Add(gridSplitter); } for (int row = 1; row < base.RowDefinitions.Count; row += 2) { gridSplitter = new GridSplitter() { AllowDrop = false, Background = Brushes.Black, ResizeDirection = GridResizeDirection.Rows, HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch }; binding = new Binding() { Source = this, Path = new PropertyPath("BorderWidth"), Mode = BindingMode.TwoWay, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged }; gridSplitter.SetBinding(GridSplitter.HeightProperty, binding); Grid.SetRow(gridSplitter, row); Grid.SetColumnSpan(gridSplitter, base.ColumnDefinitions.Count); base.Children.Add(gridSplitter); } }