绑定2个或更多依赖项属性

时间:2013-12-19 19:07:51

标签: c# wpf

我的WPF应用程序中有一个文本框,如果设置了依赖项属性,它当前正在显示。我想知道是否有办法使用OR语句显示此文本框?

<TextBox Grid.Row="1" Height="23" Width="132" Margin="451,30,0,0" 
Text="{Binding Path=PositionName}" 
Style="{StaticResource TextBoxStyleValue}" 
Visibility="{Binding IsDepProp1, Converter={StaticResource BooleanToVisibilityConverter}, 
RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" />

类似的东西:

Binding IsDepProp1 || IsDepProp2

2 个答案:

答案 0 :(得分:2)

我相信您正在寻找MultiBinding

查看:http://tech.pro/tutorial/809/wpf-tutorial-using-multibindings以获得更好的解释。

但基本上,您必须实施IMultiValueConverter并将其用于您的价值转换器。

答案 1 :(得分:1)

IMultiValueConverter是一种可行的方法,但是您可以使用两个DataTriggers来实现这一点:

<TextBox Grid.Row="1" Height="23" Width="132" Margin="451,30,0,0" 
         Text="{Binding Path=PositionName}">
    <TextBox.Style>
        <Style TargetType="TextBox" BasedOn="{StaticResource TextBoxStyleValue}">
           <Setter Property="Visibility" Value="Collapsed"/>
           <Style.Triggers>
              <DataTrigger Binding="{Binding IsDepProp1,
                               RelativeSource={RelativeSource FindAncestor,
                                       AncestorType={x:Type UserControl}}}"
                            Value="True">
                  <Setter Property="Visibility" Value="Visible"/>
              </DataTrigger>
              <DataTrigger Binding="{Binding IsDepProp2,
                              RelativeSource={RelativeSource FindAncestor,
                                      AncestorType={x:Type UserControl}}}"
                           Value="True">
                  <Setter Property="Visibility" Value="Visible"/>
               </DataTrigger>
            </Style.Triggers>
         </Style>
      </TextBox.Style>
</TextBox>

Default value will be Collapsed根据两个触发器,您可以检查if any of the property returns true, set visibility to Visible