在 DataTrigger 条件中使用绑定 [英] Using bindings in DataTrigger condition

查看:41
本文介绍了在 DataTrigger 条件中使用绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下简单的类:

Let's say I have the following simple classes:

public class Person
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class PersonHolder
{
    public Person CurrentPerson { get; set; }
    public int ActiveId { get; set; }
}

现在我有一个带有类 PersonH​​older 实例的网格为 DataContext,并且在网格中我有一个 StackPanel 绑定到 CurrentPersonPersonH​​older 的代码> - 显示其数据:

Now I have a grid with an instance of class PersonHolder as DataContext, and in the grid I have a StackPanel which is bound to the CurrentPerson of PersonHolder - showing its data:

<Grid>
    <StackPanel x:Name="PersonPanel" DataContext="{Binding CurrentPerson}">
        <TextBlock Text="{Binding Id}" />
        <TextBlock Text="{Binding Name}" />            
    </StackPanel>        
</Grid>

所以 - 对于问题:当 CurrentPerson 具有与 匹配的 Id 时,我如何给 StackPanel 一个绿色背景PersonH​​older 的 ActiveId(是的,非常有用,但这只是一个极其简化的示例..).我的想法是这是一个使用触发器的东西,但是我可以在我的触发器条件值中使用绑定吗?如果是这样,我可以做这样的事情:

So - to the question: How can I give the StackPanel a green background when the CurrentPerson has an Id matching the ActiveId of the PersonHolder (yeah, very useful, but this is just an extremely simplified example..). My thought is that this is something one uses a trigger for, but can I use bindings in my trigger condition value? If so I could do something like this:

<Grid x:Name="Foo">
    <StackPanel x:Name="PersonPanel" DataContext="{Binding CurrentPerson}">
        <TextBlock Text="{Binding Id}" />
        <TextBlock Text="{Binding Name}" />
        <StackPanel.Triggers>
            <DataTrigger Binding="{Binding Id}" Value="{Binding ElementName=Foo, Path=ActiveId}">
                <Setter TargetName="PersonPanel" Property="Background" Value="Green" />
            </DataTrigger>
        </StackPanel.Triggers>
    </StackPanel>        
</Grid>

但这行不通.我该如何解决这个问题?

But this doesn't work. How can I solve this?

推荐答案

您不能对 Value 属性使用绑定,但可以通过使用 MultiBindingIMultiValueConverter.我会在例如 Style 中定义我的 TriggerWindow.Resources,它会给出如下内容:

You can't use a binding on the Value property, but you can get around this by using a MultiBinding and an IMultiValueConverter. I would define my Trigger in a Style in e.g. the Window.Resources, which would give something like this:

<Window.Resources>
    <local:SomeMultiConverter x:Key="someMultiConverter" />
    <Style x:Key="someStyle" TargetType="StackPanel">
        <Setter Property="StackPanel.Background" Value="Red" />
        <Style.Triggers>
            <DataTrigger Value="True">
                <DataTrigger.Binding>
                    <MultiBinding Converter="{StaticResource someMultiConverter}">
                        <Binding Path="Id"></Binding>
                        <Binding ElementName="Foo" Path="DataContext.ActiveId"></Binding>
                    </MultiBinding>
                </DataTrigger.Binding>
                <Setter Property="StackPanel.Background" Value="Green" />
            </DataTrigger>
        </Style.Triggers>
    </Style> 
</Window.Resources>
<Grid x:Name="Foo">
    <StackPanel DataContext="{Binding CurrentPerson}" Style="{StaticResource someStyle}" >
        <TextBlock Text="{Binding Id}" />
        <TextBlock Text="{Binding Name}" />
    </StackPanel>
</Grid>

参见 这个链接是关于 MultiBindingIMultiValueConverter 的例子.它们相当容易编写.

See this link for an example on MultiBinding and IMultiValueConverter. They're fairly easy to write.

这篇关于在 DataTrigger 条件中使用绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆