在WPF中使用Style的问题

时间:2011-11-10 09:00:08

标签: c# wpf data-binding multibinding

我可以在xaml中使用STYLE编写以下代码吗?

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

1 个答案:

答案 0 :(得分:1)

我不确定这是否会起作用,因为我不在IDE前面,并且我正在尝试从内存中编码,但如果没有其他内容,它将作为MultiBinding的示例。

在您的资源中:

<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

在您的代码隐藏中:

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();
  }
}

修改

刚刚验证了代码,它按预期工作。