0
<Grid x:Name="LayoutRoot">
    <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
        <Button Content="Button" Width="112" Height="50" Visibility="{Binding IsFocused, ElementName=textBox1,Converter={StaticResource MyConverter1}}"/>           
    </StackPanel>
    <TextBox Height="57" HorizontalAlignment="Left" Margin="144,103,0,0" Name="textBox1" VerticalAlignment="Top" Width="98" />
    <TextBox Height="57" HorizontalAlignment="Left" Margin="277,103,0,0" x:Name="textBox2" VerticalAlignment="Top" Width="88" />
    <TextBox Height="57" HorizontalAlignment="Left" Margin="390,103,0,0" x:Name="textBox3" VerticalAlignment="Top" Width="87" />        
</Grid>

使用上面的代码,我可以得到以下结果。

在此处输入图像描述

如果单击textBox1按钮将同时隐藏,我单击textBox2textBox3按钮将隐藏。

我需要的是,每当我点击textBox1texBox3时,按钮应该是隐藏的。我的意思是上面的线,

<Button Content="Button" Width="112" Height="50" Visibility="{Binding IsFocused, ElementName=textBox1 or textBox3,Converter={StaticResource MyConverter1}}"/>

这可能吗 ?

4

1 回答 1

2

一种方法是MultiBinding与实现一起使用IMultiValueConverter

转换器的重要部分如下所示:

public class MultiConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter,
                          CultureInfo culture)
    {
        return values.Cast<bool>().Any(x => x) ?
               Visibility.Collapsed : Visibility.Visible;
    }

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

用法是这样的:

<Button Content="Button" Width="112" Height="50"
        Margin="196,186,195,75">
  <Button.Visibility>
    <MultiBinding Converter="{StaticResource MultiConverter}">
      <Binding ElementName="textBox1" Path="IsFocused" />
      <Binding ElementName="textBox3" Path="IsFocused" />
    </MultiBinding>
  </Button.Visibility>
</Button>
于 2013-02-18T10:40:41.313 回答