触发器中的 AdornedElement 属性

发布于 2024-09-04 04:50:14 字数 1050 浏览 0 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

枕梦 2024-09-11 04:50:14

您正在寻找的内容称为 MultiBinding 并内置于 WPF 中(尽管不在 Silverlight 中。)

<Grid>
    <Grid.Resources>
        <c:BooleanPairToVisibilityConverter x:Key="booleanPairToVisibility" />
    </Grid.Resources>
    <Grid.Visibility>
        <MultiBinding Converter="{StaticResource booleanPairToVisibility}">
            <Binding ElementName="errorAdorner" Path="AdornedElement.IsFocused" />
            <Binding ElementName="errorAdorner" Path="AdornedElement.IsMouseOver" />
        </MultiBinding>
    </Grid.Visibility>
</Grid>

然后您需要一个简单的 IMultiValueConverter 将这些值转换为 Visibility:

public class BooleanPairToVisibilityConverter : IMultiValueConverter {
    public object Convert( object[] values, Type targetType, object parameter, CultureInfo culture )
    {
        if ( 2 != values.Length ) throw new ArgumentException( "values" );
        return ( (bool)values[0] || (bool)values[1] ) ? Visibility.Visible : Visibility.Collapsed;
    }

    public object[] ConvertBack( object value, Type[] targetTypes, object parameter, CultureInfo culture )
    { throw new NotSupportedException(); }
}

不可否认,这并不能解决有关如何使用触发器执行此操作的第二个问题。我不会...

如果您想要围绕 ErrorDetails 元素的可见性更改进行动画,请直接在 Visibility 属性上设置触发器 - 当 MultiBinding 导致 DependencyProperty 的值发生更改时,应该调用它。此外,可能值得考虑行为来实现这一目标因为它们对于附加简单的动画来说更加简单。

What you're looking for is called a MultiBinding and is built into WPF (though not in Silverlight.)

<Grid>
    <Grid.Resources>
        <c:BooleanPairToVisibilityConverter x:Key="booleanPairToVisibility" />
    </Grid.Resources>
    <Grid.Visibility>
        <MultiBinding Converter="{StaticResource booleanPairToVisibility}">
            <Binding ElementName="errorAdorner" Path="AdornedElement.IsFocused" />
            <Binding ElementName="errorAdorner" Path="AdornedElement.IsMouseOver" />
        </MultiBinding>
    </Grid.Visibility>
</Grid>

Then you need a simple IMultiValueConverter to translate those values into a Visibility:

public class BooleanPairToVisibilityConverter : IMultiValueConverter {
    public object Convert( object[] values, Type targetType, object parameter, CultureInfo culture )
    {
        if ( 2 != values.Length ) throw new ArgumentException( "values" );
        return ( (bool)values[0] || (bool)values[1] ) ? Visibility.Visible : Visibility.Collapsed;
    }

    public object[] ConvertBack( object value, Type[] targetTypes, object parameter, CultureInfo culture )
    { throw new NotSupportedException(); }
}

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.

清浅ˋ旧时光 2024-09-11 04:50:14

我也遇到过类似的情况,我想使用触发器。和你一样,我无法让它与常规的一起工作。但我最终发现您可以使用 来触发装饰元素的属性。 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文