using System.Collections.Generic;
|
using System.Windows;
|
using System.Windows.Controls;
|
using System.Windows.Documents;
|
using System.Windows.Media;
|
|
namespace PdmSwPlugin.Commmon.Util.UI
|
{
|
public class MaskAdorner : Adorner
|
{
|
private UIElement child;
|
private TextBox textBox;
|
|
public static readonly Dictionary<Visual, MaskAdorner> cache = new Dictionary<Visual, MaskAdorner>();
|
|
public MaskAdorner(UIElement adornedElement, string message = null) : base(adornedElement)
|
{
|
textBox = new TextBox
|
{
|
TextAlignment = TextAlignment.Center,
|
Background = new SolidColorBrush(Color.FromArgb(0, 0, 0, 0)),
|
Text = message,
|
BorderThickness = new Thickness(0)
|
};
|
StackPanel stackPanel = new StackPanel()
|
{
|
HorizontalAlignment = HorizontalAlignment.Center,
|
VerticalAlignment = VerticalAlignment.Center,
|
Background = new SolidColorBrush(Color.FromArgb(255, 255, 255, 255)),
|
};
|
stackPanel.Children.Add(new ProgressBar
|
{
|
Width = 100,
|
Height = 20,
|
IsIndeterminate = true
|
});
|
stackPanel.Children.Add(textBox);
|
Child = new Border
|
{
|
Background = new SolidColorBrush(Color.FromArgb(100, 0, 0, 0)),
|
HorizontalAlignment = HorizontalAlignment.Stretch,
|
VerticalAlignment = VerticalAlignment.Stretch,
|
Child = stackPanel
|
};
|
}
|
//Adorner 直接继承自 FrameworkElement,
|
//没有Content和Child属性,
|
//自己添加一个,方便向其中添加我们的控件
|
public UIElement Child
|
{
|
get => child;
|
set
|
{
|
if (value == null)
|
{
|
RemoveVisualChild(child);
|
}
|
else
|
{
|
AddVisualChild(value);
|
}
|
child = value;
|
}
|
}
|
//重写VisualChildrenCount 表示此控件只有一个子控件
|
protected override int VisualChildrenCount => 1;
|
//控件计算大小的时候,我们在装饰层上添加的控件也计算一下大小
|
protected override Size ArrangeOverride(Size finalSize)
|
{
|
child?.Arrange(new Rect(finalSize));
|
return finalSize;
|
}
|
//重写GetVisualChild,返回我们添加的控件
|
protected override Visual GetVisualChild(int index)
|
{
|
if (index == 0 && child != null) return child;
|
return base.GetVisualChild(index);
|
}
|
|
public void ShowMessage(string message)
|
{
|
this.textBox.Text = message;
|
}
|
|
|
public static void ShowMask(Visual visual, string message = null)
|
{
|
var sb = cache;
|
if (cache.ContainsKey(visual))
|
{
|
return;
|
}
|
visual.Dispatcher.Invoke(() =>
|
{
|
AdornerLayer adornerLayer = AdornerLayer.GetAdornerLayer(visual);
|
if (adornerLayer == null) return;
|
Adorner[] ads = adornerLayer.GetAdorners(adornerLayer);
|
if (ads != null)
|
{
|
foreach (Adorner ad in ads)
|
{
|
if (ad is MaskAdorner adorner)
|
{
|
return;
|
}
|
}
|
}
|
//创建我们定义的Adorner
|
MaskAdorner _adorner = new MaskAdorner(adornerLayer, message);
|
//添加到adornerLayer装饰层上
|
adornerLayer.Add(_adorner);
|
cache.Add(visual, _adorner);
|
});
|
}
|
|
public static void ShowMessage(Visual visual, string message)
|
{
|
if (!cache.ContainsKey(visual))
|
{
|
return;
|
}
|
visual.Dispatcher.Invoke(() =>
|
{
|
AdornerLayer adornerLayer = AdornerLayer.GetAdornerLayer(visual);
|
MaskAdorner mask = cache[visual];
|
if (mask == null)
|
{
|
return;
|
}
|
mask.ShowMessage(message);
|
});
|
}
|
|
public static void HideMask(Visual visual)
|
{
|
if (!cache.ContainsKey(visual))
|
{
|
return;
|
}
|
visual.Dispatcher.Invoke(() =>
|
{
|
if (cache.ContainsKey(visual))
|
{
|
AdornerLayer adornerLayer = AdornerLayer.GetAdornerLayer(visual);
|
adornerLayer.Remove(cache[visual]);
|
cache.Remove(visual);
|
}
|
//Adorner[] ads = adornerLayer.GetAdorners(adornerLayer);
|
//if (ads != null)
|
//{
|
// foreach (Adorner ad in ads)
|
// {
|
// if (ad is MaskAdorner)
|
// {
|
// adornerLayer.Remove(ad);
|
// }
|
// }
|
//}
|
});
|
}
|
}
|
}
|