C# WPF绑定:!价值

C# WPF绑定:!价值,c#,wpf,binding,C#,Wpf,Binding,我有一个按钮: <Button Content="Stop loading" /> 在ViewModel中,我已加载属性。我不想写属性IsNotLoaded,但我想在绑定中使用IsLoaded,并在IsLoaded=true时禁用按钮 如何实现这样的功能: <Button Content="Stop loading" IsEnabled="{Binding !IsLoaded}" /> 另外,如果它比编写附加属性更难,我将使用IsNotLoaded属性。执行此

我有一个按钮:

<Button Content="Stop loading" />

在ViewModel中,我已加载属性。我不想写属性IsNotLoaded,但我想在绑定中使用IsLoaded,并在IsLoaded=true时禁用按钮

如何实现这样的功能:

<Button Content="Stop loading" IsEnabled="{Binding !IsLoaded}" />


另外,如果它比编写附加属性更难,我将使用IsNotLoaded属性。

执行此操作的标准方法是生成一个将反转布尔值的函数。虽然创建此类比添加新属性更困难,但它是完全可重用的-因此以后,您可以在其他绑定中重用它(而不会用大量!属性属性污染ViewModel)

这个类看起来像:

[ValueConversion(typeof(bool), typeof(bool))]
public class InvertBoolConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        bool booleanValue = (bool)value;
        return !booleanValue;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        bool booleanValue = (bool)value;
        return !booleanValue;
    }
}
    public ICommand CancelLoadingCommand
    {
        get
        {
            if (_cancelLoadingCommand== null)
            {
                this._cancelLoadingCommand= new RelayCommand(
                    delegate
                    {
                        // Cancel the loading process.
                    },
                    delegate
                    {
                        return !this.IsLoaded;
                    }
                );
            }

            return _cancelLoadingCommand;
        }
    }
然后,您可以将其添加到您的资源中:

<src:InvertBoolConverter x:Key="invertBoolConverter"/>

一旦你有了这个,你会像这样使用它:

<Button Content="Stop loading" 
        IsEnabled="{Binding IsLoaded, Converter={StaticResource invertBoolConverter}}" 
/>

一种解决方案是使用转换器反转布尔值。差不多

public class InvertedBoolenConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return !(bool)value;

    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return !(bool)value;
    }
}
然后将转换器添加到某个资源中,并在绑定中使用它:

<YourUserControl.Resources>
   <c:InvertedBoolenConverter x:Key="InvertedBoolenConverter" />
</YourUserControl.Resources>

<Button Content="Stop loading" IsEnabled="{Binding IsLoaded,Converter={StaticResource InvertedBoolenConverter}}" />

您想使用转换器。这里有一个可以帮你的

  public class booleaninverter : IValueConverter
  {
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
      return !(bool)value;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
      return !(bool)value;
    }
  }
要使用它,请像这样编写xaml

<Button Content="Stop loading" IsEnabled="{Binding IsLoaded, Converter={StaticResource booleaninverter}" />

您可以在App.xaml或其他窗口/控件资源部分创建静态资源。当然,您必须进行“本地”名称空间声明等等,但这是为您完成的大部分工作

<local:booleaninverter x:key="booleaninverter"/>

虽然转换器的答案都是有效的,但您可能想看看另一种方法:命令

在WPF中(在Silverlight中),可以将ICommand绑定到该按钮。因此,如果您在ViewModel上创建了一个名为CancelLoadingCommand的属性,该属性实现了ICommand,那么您可以按如下方式绑定按钮:

<Button Content="Stop Loading" Command="{Binding CancelLoadingCommand}" />
注意,我在这里使用的是RelayCommand(它是PRISM或MVVM Light框架的一部分)。我建议也调查其中一个


我希望这能有所帮助。

你会认为微软会提供其中之一。他们已经提供了BooleanToVisibilityConverter。为什么没有其他一些有用的呢?