2016-03-31 14 views
5

Sono a conoscenza delle proprietà IsOverflowOpen e HasOverflowItems ma sto cercando un modo per dire se l'elemento (pulsante, radiobutton ...) è stato spostato in ToolBarOverflowPanel in modo che io possa usa un grilletto per cambiare il suo stile.WPF ToolBar - Rileva quando l'oggetto è impostato in ToolBarOverflowPanel

Ho bisogno di questo per poter riprodurre alcuni stili UWP ToolBar (Windows 10 Mail, Word, Excel ...). Ho riprodotto con successo la maggior parte dello stile e l'unico bit mancante è quello di poter cambiare lo stile del mio oggetto quando è nel pannello di overflow.

Nello screenshot di ciò che sto cercando di riprodurre, è possibile vedere chiaramente che i pulsanti Identità speciale e Spaziatura linea hanno cambiato stile in base al fatto che siano visualizzati o in overflow. Windows 10 ToolBar Style Screenshot

risposta

5

Non è possibile farlo solo con xaml. È necessario utilizzare il codice o creare alcune proprietà associate.

Ecco la soluzione con l'AttachedProperty:

prima cosa è necessario creare una classe helper esponendo 2 proprietà:

  • L'IsInOverflowPanel Proprietà di sola lettura che verrà utilizzato per innescare il cambiamento stile.
  • La proprietà TrackParentPanel, che è il meccanismo di abilitazione/disabilitazione.

Ecco l'attuazione:

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); 
    } 
} 

Poi, per ogni oggetti che hanno bisogno di cambiare stile di effettuare le seguenti operazioni:

<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> 
Problemi correlati