Ravi Makwana
Ravi Makwana

Reputation: 15

issue with using Style in WPF

Can I write following code using STYLE in xaml?

cmbEnquiry.IsEnabled = (txtQuotationNo.IsEnabled && txtQuotationNo.IsReadOnly == false);

Upvotes: 0

Views: 66

Answers (1)

K Mehta
K Mehta

Reputation: 10543

I'm not sure if this will work as is as I'm not in front of an IDE and am trying to code from memory, but if nothing else, it'll serve as an example for MultiBinding.

In your resources:

<local:AndNotConverter x:Key="AndNotConverter" />
<Style ...>
    <Setter Property="IsEnabled">
        <Setter.Value>
            <MultiBinding Converter="{StaticResource AndNotConverter}">
                <Binding ElementName="txtQuotationNo" Path="IsEnabled" />
                <Binding ElementName="txtQuotationNo" Path="IsReadOnly" />
            </MultiBinding>
        </Setter.Value>
    </Setter>
</Style

In your code-behind:

public class AndNotConverter : IMultiValueConverter
{
  public object Convert(object[] values, Type targetType, object parameter, 
      System.Globalization.CultureInfo culture)
  {
      return (bool)values[0] && !((bool)values[1]);
  }

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

Edit:

Just verified the code, and it works as expected.

Upvotes: 1

Related Questions