You can use a MultiBinding for that purpose. You can create an IMultiValueConverter as follows:
public class HasAllTextConverter: IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
bool res = true;
foreach (object val in values)
{
if (string.IsNullOrEmpty(val as string))
{
res = false;
}
}
return res;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
and, use it with MultiBinding
as follows:
<StackPanel>
<StackPanel.Resources>
<local:HasAllTextConverter x:Key="HasAllTextConverter" />
</StackPanel.Resources>
<TextBox Name="tb1" />
<TextBox Name="tb2" />
<Button Content="Button 1" >
<Button.IsEnabled>
<MultiBinding Converter="{StaticResource HasAllTextConverter}">
<Binding ElementName="tb1" Path="Text" />
<Binding ElementName="tb2" Path="Text" />
</MultiBinding>
</Button.IsEnabled>
</Button>
</StackPanel>
[EDIT: Add a solution without code-behind.]
If you don't want to use code-behind, you can do it with a MultiTrigger
(but, since panel's triggers can contain only an EventTrigger
, you have to wrap it with a DataTemplate
or something else that can contain it and, contains the involved elements too). Something like:
<ContentControl Content="{Binding}">
<ContentControl.ContentTemplate>
<DataTemplate>
<StackPanel>
<TextBox Name="tb3" />
<TextBox Name="tb4" />
<Button Name="btn2" Content="Button 2" />
</StackPanel>
<DataTemplate.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition SourceName="tb3" Property="TextBox.Text" Value="" />
<Condition SourceName="tb4" Property="TextBox.Text" Value="" />
</MultiTrigger.Conditions>
<Setter TargetName="btn2" Property="IsEnabled" Value="False" />
</MultiTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</ContentControl.ContentTemplate>
</ContentControl>