【问题标题】:WPF Binding: !value [duplicate]WPF 绑定:!值 [重复]
【发布时间】:2011-06-17 08:44:39
【问题描述】:

我有按钮:

<Button Content="Stop loading" />

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

如何实现这样的:

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

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

【问题讨论】:

标签: c# wpf binding


【解决方案1】:

执行此操作的标准方法是创建一个IValueConverter,它将反转您的布尔值。虽然创建这个类比添加一个新属性更困难,但它是完全可重用的 - 所以以后,您可以在其他绑定中重用它(不会用大量 !Property 属性污染您的 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;
    }
}

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

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

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

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

【讨论】:

  • 你会认为微软会提供其中之一。他们已经提供了 BooleanToVisibilityConverter。为什么不推荐一些其他有用的?
【解决方案2】:

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

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

<Button Content="Stop Loading" Command="{Binding CancelLoadingCommand}" />

您的 ViewModel 中 CancelLoadingCommand 的实现如下所示:

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

            return _cancelLoadingCommand;
        }
    }

注意,我在这里使用 RelayCommand(它是 PRISM 或 MVVM-Light 框架的一部分)。我建议您也研究其中之一。

我希望这会有所帮助。

【讨论】:

    【解决方案3】:

    一种解决方案是使用转换器来反转布尔值。类似的东西

    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}}" />
    

    【讨论】:

      【解决方案4】:

      您想使用转换器。这是一个可以为您解决问题的方法。

        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"/>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-08-02
        • 2019-03-01
        • 1970-01-01
        • 2010-11-13
        • 2011-12-16
        相关资源
        最近更新 更多