2016-03-31 15 views
5

Tôi biết về các thuộc tính IsOverflowOpen và HasOverflowItems nhưng tôi đang tìm cách để biết liệu mục (nút, radiobutton ...) đã chuyển vào ToolBarOverflowPanel để tôi có thể sử dụng trình kích hoạt để thay đổi kiểu của nó.WPF ToolBar - Phát hiện khi mục được đặt thành ToolBarOverflowPanel

Tôi cần điều này để có thể tạo lại một số kiểu thanh công cụ UWP (Windows 10 Mail, Word, Excel ...). Tôi đã tái tạo thành công hầu hết các phong cách và bit chỉ mất tích là để có thể thay đổi phong cách của mục của tôi khi nó nằm trong bảng điều khiển tràn.

Trên ảnh chụp màn hình của những gì tôi đang cố gắng tái tạo, bạn có thể thấy rõ rằng các nút Đặc biệt và Giãn cách dòng đã thay đổi kiểu dựa vào việc chúng được hiển thị hay bị tràn. Windows 10 ToolBar Style Screenshot

Trả lời

5

Bạn không thể làm điều đó chỉ với xaml. Bạn phải sử dụng mã phía sau hoặc tạo một số thuộc tính được đính kèm.

Dưới đây là giải pháp với AttachedProperty:

Trước tiên, bạn cần phải tạo một lớp helper lộ 2 thuộc tính:

  • Các IsInOverflowPanel tài sản mà bạn sẽ sử dụng để kích hoạt sự thay đổi phong cách read-only.
  • Thuộc tính TrackParentPanel, là cơ chế bật/tắt.

Đây là việc thực hiện:

public static class ToolBarHelper 
{ 
    public static readonly DependencyPropertyKey IsInOverflowPanelKey = 
     DependencyProperty.RegisterAttachedReadOnly("IsInOverflowPanel", typeof(bool), typeof(ToolBarHelper), new PropertyMetadata(false)); 

    public static readonly DependencyProperty IsInOverflowPanelProperty = IsInOverflowPanelKey.DependencyProperty; 

    [AttachedPropertyBrowsableForType(typeof(UIElement))] 
    public static bool GetIsInOverflowPanel(UIElement target) 
    { 
     return (bool)target.GetValue(IsInOverflowPanelProperty); 
    } 

    public static readonly DependencyProperty TrackParentPanelProperty = 
     DependencyProperty.RegisterAttached("TrackParentPanel", typeof(bool), typeof(ToolBarHelper), 
              new PropertyMetadata(false, OnTrackParentPanelPropertyChanged)); 

    public static void SetTrackParentPanel(DependencyObject d, bool value) 
    { 
     d.SetValue(TrackParentPanelProperty, value); 
    } 

    public static bool GetTrackParentPanel(DependencyObject d) 
    { 
     return (bool)d.GetValue(TrackParentPanelProperty); 
    } 

    private static void OnTrackParentPanelPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
    { 
     var element = d as UIElement; 
     if (element != null) 
     { 
      bool newValue = (bool)e.NewValue; 
      if (newValue) 
      { 
       element.LayoutUpdated += (s, arg) => OnControlLayoutUpdated(element); 
      } 
     } 
    } 
    private static void OnControlLayoutUpdated(UIElement element) 
    { 
     var isInOverflow = TreeHelper.FindParent<ToolBarOverflowPanel>(element) != null; 
     element.SetValue(IsInOverflowPanelKey, isInOverflow); 
    } 
} 

public static class TreeHelper 
{ 
    public static T FindParent<T>(this DependencyObject obj) where T : DependencyObject 
    { 
     return obj.GetAncestors().OfType<T>().FirstOrDefault(); 
    } 

    public static IEnumerable<DependencyObject> GetAncestors(this DependencyObject element) 
    { 
     do 
     { 
      yield return element; 
      element = VisualTreeHelper.GetParent(element); 
     } while (element != null); 
    } 
} 

Sau đó, cho tất cả các mặt hàng mà cần phải thay đổi phong cách làm như sau:

<Button x:Name="DeleteButton" Content="Delete" helpers:ToolBarHelper.TrackParentPanel="True"> 
    <Button.Style> 
     <Style BasedOn="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" TargetType="{x:Type Button}"> 
      <Style.Triggers> 
       <Trigger Property="helpers:ToolBarHelper.IsInOverflowPanel" Value="True"> 
        <!-- The Overflow style setters --> 
       </Trigger> 
      </Style.Triggers> 
     </Style> 
    </Button.Style> 
</Button> 
Các vấn đề liên quan