WPF绑定:!值

Rov*_*ver 19 c# wpf binding

我有按钮:

<Button Content="Stop loading" />
Run Code Online (Sandbox Code Playgroud)

在ViewModel中,我有属性IsLoaded.我不想写属性IsNotLoaded但我想在IsLoaded = true时在绑定和禁用按钮中使用IsLoaded.

如何实现这样的事情:

<Button Content="Stop loading" IsEnabled="{Binding !IsLoaded}" />
Run Code Online (Sandbox Code Playgroud)

PS如果比编写附加属性更难,我将使用IsNotLoaded属性.

Ree*_*sey 26

执行此操作的标准方法是使其IValueConverter反转您的布尔值.虽然创建此类比添加新属性更困难,但它完全可以重用 - 所以稍后,您可以在其他绑定中重复使用它(不会使用大量的!属性来污染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;
    }
}
Run Code Online (Sandbox Code Playgroud)

然后,您将其添加到您的资源:

<src:InvertBoolConverter x:Key="invertBoolConverter"/>
Run Code Online (Sandbox Code Playgroud)

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

<Button Content="Stop loading" 
        IsEnabled="{Binding IsLoaded, Converter={StaticResource invertBoolConverter}}" 
/>
Run Code Online (Sandbox Code Playgroud)


Ser*_*ioL 11

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

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

<Button Content="Stop Loading" Command="{Binding CancelLoadingCommand}" />
Run Code Online (Sandbox Code Playgroud)

ViewModel中CancelLoadingCommand的实现如下所示:

    public ICommand CancelLoadingCommand
    {
        get
        {
            if (_cancelLoadingCommand== null)
            {
                this._cancelLoadingCommand= new RelayCommand(
                    delegate
                    {
                        // Cancel the loading process.
                    },
                    delegate
                    {
                        return !this.IsLoaded;
                    }
                );
            }

            return _cancelLoadingCommand;
        }
    }
Run Code Online (Sandbox Code Playgroud)

注意,我在这里使用RelayCommand(它是PRISM或MVVM-Light框架的一部分).我建议也要考虑其中一个.

我希望这有帮助.


Jam*_*Hay 7

一种解决方案是使用转换器来反转布尔值.就像是

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

然后将转换器添加到某个地方的资源并在绑定中使用它:

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

<Button Content="Stop loading" IsEnabled="{Binding IsLoaded,Converter={StaticResource InvertedBoolenConverter}}" />
Run Code Online (Sandbox Code Playgroud)


A.R*_*.R. 6

你想使用转换器.这是一个可以为你做的伎俩.

  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;
    }
  }
Run Code Online (Sandbox Code Playgroud)

要使用它,请像这样编写你的xaml

<Button Content="Stop loading" IsEnabled="{Binding IsLoaded, Converter={StaticResource booleaninverter}" />
Run Code Online (Sandbox Code Playgroud)

您可以在App.xaml或其他窗口/控制资源部分中创建静态资源.当然,您必须制作"本地"命名空间声明和什么不是,但这是为您完成的大部分工作.

<local:booleaninverter x:key="booleaninverter"/>
Run Code Online (Sandbox Code Playgroud)