触发器中的 AdornedElement 属性
我在 XAML 中有一个 Adorner,用于 ErrorValidation。基本上我有一个网格,我想在两种条件下显示(如果“AdornedElement”IsFocused或IsMouseOver)。
下面是我成功绑定到 AdornedElement 的 IsFocused 的代码片段,但正如您所知,这只解决了 1/2 的条件。现在,我无法将另一个绑定传递到转换器中,也无法创建处理这两者的属性(需要是仅限 XAML 的解决方案)。
<AdornedElementPlaceholder
x:Name="errorAdorner" />
...
<Grid
x:Name="ErrorDetails"
Visibility="{Binding ElementName=errorAdorner, Path=AdornedElement.IsFocused, Converter={StaticResource BooleanToVisibilityConverter}}" />
...
我想做的是使用触发器来处理这个问题,唯一的问题是我无法访问触发器上的 AdornedElement 的属性。
像这样的东西......
<Trigger
SourceName="errorAdorner"
Property="AdornedElement.IsFocused"
Value="True">
<Setter
TargetName="ErrorDetails"
Property="Visibility"
Value="Visible" />
</Trigger>
这也会有所帮助,因为我想做的是触发动画,而不仅仅是设置可见性。
任何帮助都会很棒。
I have an Adorner in XAML that I'm using for ErrorValidation. Basically I have a grid that I want to display on two conditions (if the "AdornedElement" IsFocused or IsMouseOver).
Below is a code snippet where I'm binding - successfully - to the IsFocused of the AdornedElement, but as you can tell that only solves 1/2 the conditions. Now I can't pass another binding into the converter, nor can I create a property that handles both (needs to be XAML only solution).
<AdornedElementPlaceholder
x:Name="errorAdorner" />
...
<Grid
x:Name="ErrorDetails"
Visibility="{Binding ElementName=errorAdorner, Path=AdornedElement.IsFocused, Converter={StaticResource BooleanToVisibilityConverter}}" />
...
What I want to do is use triggers to handle this, the only problem is I can't access the AdornedElement's properties on a trigger.
Something like this ...
<Trigger
SourceName="errorAdorner"
Property="AdornedElement.IsFocused"
Value="True">
<Setter
TargetName="ErrorDetails"
Property="Visibility"
Value="Visible" />
</Trigger>
This would also help as part of what I want to do is trigger animations, rather than just setting the visibility.
Any help would be great.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在寻找的内容称为 MultiBinding 并内置于 WPF 中(尽管不在 Silverlight 中。)
然后您需要一个简单的 IMultiValueConverter 将这些值转换为 Visibility:
不可否认,这并不能解决有关如何使用触发器执行此操作的第二个问题。我不会...
如果您想要围绕 ErrorDetails 元素的可见性更改进行动画,请直接在 Visibility 属性上设置触发器 - 当 MultiBinding 导致 DependencyProperty 的值发生更改时,应该调用它。此外,可能值得考虑行为来实现这一目标因为它们对于附加简单的动画来说更加简单。
What you're looking for is called a MultiBinding and is built into WPF (though not in Silverlight.)
Then you need a simple IMultiValueConverter to translate those values into a Visibility:
Admittedly, this doesn't solve the second question about how to do this with Triggers. I wouldn't...
If you want an animation around a change in the ErrorDetails element's visibility, set the trigger on the visibility property directly - it should get invoked when the MultiBinding causes the DependencyProperty's value to change. Also, it might be worth considering Behaviors to accomplish that instead as they're a little more straightforward for attaching simple animations.
我也遇到过类似的情况,我想使用触发器。和你一样,我无法让它与常规的
一起工作。但我最终发现您可以使用
来触发装饰元素的属性。 Marco Zhou 对此问题的回答 显示要使用的语法。此答案也展示了这种方法,并包括由于触发器而触发的动画。
I had a similar situation where I wanted to use a Trigger. Like you, I could not get it to work with a regular
<Trigger>
. But I finally found that you can use a<DataTrigger>
to trigger on a property of the adorned element. Marco Zhou's answer to this question shows the syntax to use.This answer also shows this approach, and includes animations being fired as a result of the trigger.