Using bindings in DataTrigger condition

倖福魔咒の 提交于 2019-11-28 20:33:01
code-zoop

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>

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!