我知道IsOverflowOpen和HasOverflowItems属性,但我正在寻找一种方法来判断项目(按钮,单选按钮......)是否已移入ToolBarOverflowPanel,因此我可以使用触发器来更改其样式。
我需要这个才能重现一些UWP工具栏样式(Windows 10 Mail,Word,Excel ......)。我已经成功地复制了大部分风格,唯一缺少的是能够在溢出面板中更改我的项目的样式。
答案 0 :(得分:5)
您无法仅使用xaml执行此操作。您必须使用后面的代码或创建一些附加属性。
以下是AttachedProperty的解决方案:
首先,您需要创建一个暴露2个属性的辅助类:
以下是实施:
UPDATE cities C, staions S
SET C.station_id = S.id
WHERE (((acos(sin((S.station_latitude*pi()/180)) * sin((city_latitude*pi()/180)) + cos((S.station_latitude *pi()/180)) * cos((city_latitude*pi()/180)) * cos(((S.station_longitude - city_longitude)*pi()/180))))*180/pi())*60*1.1515*1.609344) < 30
然后,对于需要更改样式的每个项目,请执行以下操作:
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);
}
}