好的,所以我之前收到Here的答案非常有效。但是,在已知问题中,它提到以下内容:
另外,这不会立即在UserControls内部起作用,因为
AdornerLayer.GetAdornerLayer(AdornedElement)
将返回null
在UserControls中。可以通过寻找
AdornerLayer
的父项的UserControl
(或
父级(递归)。周围有这样做的功能。
所以,我大部分时候都在使用很棒的代码,但是当我尝试在tabcontrol内的一个元素上使用它时,我遇到了一个问题。模糊效果仅在TabItem内才有效,而不是期望的效果,而不是整个窗口。此外,tabItem的内容似乎作为可视笔刷被打印了几次。这是一个例子。 Custom Decorator包裹在包含2个文本块的堆栈面板周围,其中一个包含“ GP:”,另一个包含一个数字。这是在选项卡项中应用此外观时的前后照片:
那么,我该如何纠正呢?
我将在此处发布我的代码段,因为自回答以来我对它们进行了一些修改(尽管不是“破坏”了它)
这是带有装饰器的XAML:
<models:TipFocusDecorator IsOpen="{Binding TutorialBoolGP}" TipHead="{Binding TutorialTitle}" TipText="{Binding TutorialDescription}" TipPos="{Binding TutorialPosition}">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Background="Transparent">
<TextBlock Text="GP: " />
<TextBlock Text="{Binding PlayerGP, Converter={StaticResource IntToComma}}" />
</StackPanel>
</models:TipFocusDecorator>
装饰器:
public class TipFocusDecorator : Decorator
{
public bool IsOpen
{
get { return (bool)GetValue(IsOpenProperty); }
set { SetValue(IsOpenProperty, value); }
}
// Using a DependencyProperty as the backing store for Open. This enables animation, styling, binding, etc...
public static readonly DependencyProperty IsOpenProperty =
DependencyProperty.Register("IsOpen", typeof(bool), typeof(TipFocusDecorator),
new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, IsOpenPropertyChanged));
public string TipText
{
get { return (string)GetValue(TipTextProperty); }
set { SetValue(TipTextProperty, value); }
}
// Using a DependencyProperty as the backing store for TipText. This enables animation, styling, binding, etc...
public static readonly DependencyProperty TipTextProperty =
DependencyProperty.Register("TipText", typeof(string), typeof(TipFocusDecorator), new UIPropertyMetadata(string.Empty));
public string TipHead
{
get { return (string)GetValue(TipHeadProperty); }
set { SetValue(TipHeadProperty, value); }
}
// Using a DependencyProperty as the backing store for TipText. This enables animation, styling, binding, etc...
public static readonly DependencyProperty TipHeadProperty =
DependencyProperty.Register("TipHead", typeof(string), typeof(TipFocusDecorator), new UIPropertyMetadata(string.Empty));
public string TipPos
{
get { return (string)GetValue(TipPosProperty); }
set { SetValue(TipPosProperty, value); }
}
// Using a DependencyProperty as the backing store for TipPos. This enables animation, styling, binding, etc...
public static readonly DependencyProperty TipPosProperty =
DependencyProperty.Register("TipPos", typeof(string), typeof(TipFocusDecorator), new UIPropertyMetadata(string.Empty));
public bool HasBeenShown
{
get { return (bool)GetValue(HasBeenShownProperty); }
set { SetValue(HasBeenShownProperty, value); }
}
// Using a DependencyProperty as the backing store for HasBeenShown. This enables animation, styling, binding, etc...
public static readonly DependencyProperty HasBeenShownProperty =
DependencyProperty.Register("HasBeenShown", typeof(bool), typeof(TipFocusDecorator), new UIPropertyMetadata(false));
private static void IsOpenPropertyChanged(object sender, DependencyPropertyChangedEventArgs e)
{
var decorator = sender as TipFocusDecorator;
if ((bool)e.NewValue)
{
if (!decorator.HasBeenShown)
decorator.HasBeenShown = true;
decorator.Open();
}
if (!(bool)e.NewValue)
{
decorator.Close();
}
}
TipFocusAdorner adorner;
protected void Open()
{
adorner = new TipFocusAdorner(this.Child);
var adornerLayer = AdornerLayer.GetAdornerLayer(this.Child);
adornerLayer.Add(adorner);
TutorialTip tip = new TutorialTip(TipHead,TipText,TipPos);
tip.Owner = Application.Current.MainWindow;
double width = tip.Width;
double height = tip.Height;
Point position = this.Child.PointToScreen(new Point(0d, 0d));
switch (TipPos)
{
case "Bottom":
position.X += (this.Child.RenderSize.Width / 2) - (width / 2);
position.Y += this.Child.RenderSize.Height + 10;
break;
case "Top":
position.X += (this.Child.RenderSize.Width / 2) - (width / 2);
position.Y += -height - 10;
break;
case "Left":
position.X += -width - 10;
position.Y += (this.Child.RenderSize.Height / 2) - (height / 2);
break;
case "Right":
position.X += this.Child.RenderSize.Width + 10;
position.Y += (this.Child.RenderSize.Height / 2) - (height / 2);
break;
}
tip.Left = position.X;
tip.Top = position.Y;
tip.ShowDialog();
//MessageBox.Show(TipText + position); // Change for your custom tip Window
IsOpen = false;
}
protected void Close()
{
var adornerLayer = AdornerLayer.GetAdornerLayer(this.Child);
adornerLayer.Remove(adorner);
adorner = null;
}
}
最后是Adorner:
public class TipFocusAdorner : Adorner
{
public TipFocusAdorner(UIElement adornedElement)
: base(adornedElement)
{
}
protected override void OnRender(System.Windows.Media.DrawingContext drawingContext)
{
base.OnRender(drawingContext);
var root = Window.GetWindow(this);
var adornerLayer = AdornerLayer.GetAdornerLayer(AdornedElement);
var presentationSource = PresentationSource.FromVisual(adornerLayer);
Matrix transformToDevice = presentationSource.CompositionTarget.TransformToDevice;
var sizeInPixels = transformToDevice.Transform((Vector)adornerLayer.RenderSize);
RenderTargetBitmap rtb = new RenderTargetBitmap((int)(sizeInPixels.X), (int)(sizeInPixels.Y), 96, 96, PixelFormats.Default);
var oldEffect = root.Effect;
var oldVisibility = AdornedElement.Visibility;
root.Effect = new BlurEffect();
AdornedElement.SetCurrentValue(FrameworkElement.VisibilityProperty, Visibility.Hidden);
rtb.Render(root);
AdornedElement.SetCurrentValue(FrameworkElement.VisibilityProperty, oldVisibility);
root.Effect = oldEffect;
drawingContext.DrawImage(rtb, adornerLayer.TransformToVisual(AdornedElement).TransformBounds(new Rect(adornerLayer.RenderSize)));
drawingContext.DrawRectangle(new SolidColorBrush(Color.FromArgb(22, 0, 0, 0)), null, adornerLayer.TransformToVisual(AdornedElement).TransformBounds(new Rect(adornerLayer.RenderSize)));
drawingContext.DrawRectangle(new VisualBrush(AdornedElement) { AlignmentX = AlignmentX.Left, TileMode = TileMode.None, Stretch = Stretch.None },
null,
AdornedElement.RenderTransform.TransformBounds(new Rect(AdornedElement.RenderSize)));
}
}
最佳答案
AdornerLayer.GetAdornerLayer检索给定元素上方(父级)的装饰层。因此,在UserControl内部返回null不一定是正确的。只要在UserControl上方存在AdornerLayer,它就会返回该值。 Window默认情况下会创建一个AdornerLayer,但仅在加载后。
实际上,我使用简单的方法测试了您的代码
<Grid x:Name="Container">
<DockPanel>
<TextBlock DockPanel.Dock="Top">Outside of Tab</TextBlock>
<TabControl x:Name="TabControl">
<TabItem Header="Here">
<local:UserControlContainingTipFocus/>
</TabItem>
</TabControl>
</DockPanel>
</Grid>
我无法重现该问题。模糊应用于窗口内的所有内容
因此,在您的情况下,必须有创建AdornerLayer的UserControl的父级。我猜在TabControl或TabItem。您可以使用Snoop进行检查。
但不必担心,您可以创建自己的AdornerLayer。只需将要模糊的元素放在AdornerDecorator中即可。
<Window >
<AdornerDecorator>
<Grid x:Name="Container">
<DockPanel>
<TextBlock DockPanel.Dock="Top">Outside of adorner</TextBlock>
<TabControl x:Name="TabControl">
<TabItem Header="Here">
<local:TestControl></local:TestControl>
</TabItem>
</TabControl>
</DockPanel>
</Grid>
</AdornerDecorator>
</Window>
然后修改对AdornerLayer.GetAdornerLayer的每次调用。不要传递原始元素,而是传递要模糊的容器。在我的示例中,它可以是网格,也可以是AdornerDecorator本身。
var root = Window.GetWindow(this);
var blurContainer = (Visual) root.Content;
var adornerLayer = AdornerLayer.GetAdornerLayer(blurContainer);
上面的代码使用Window.GetWindow并访问其内容(第一个孩子)。但是您可以轻松地在TipFocusAdorner / Decorator中创建一个属性,以指定要传递给AdornerLayer.GetAdornerLayer的元素
关于c# - 返回null时如何递归选择AdornedElement的父级,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31845295/