【问题标题】:INotifyPropertyChanged and Button.IsEnabledINotifyPropertyChanged 和 Button.IsEnabled
【发布时间】:2016-03-17 13:28:00
【问题描述】:

我有 View 及其 ViewModel。在 View 中,我有一些面板绑定到我的 ViewModel 中的某些属性;

例如第一个属性是SomeObject。在那个面板中,我有一些文本框,它们绑定到 SomeObject 类中的一些属性。 ViewModel 和类中的所有绑定都实现了 INPC。

面板中的那个文本框以这种方式绑定到SomeObject 类中的属性:SomeObject.Property

在那个视图中,我有一个保存按钮。每次在文本框中输入内容或在面板中更改时,我都需要更改按钮的 IsEnabled 属性。

我已经通过从 SomeObject 的 setter 更改绑定到按钮的 IsEnabled 属性,仅为我的视图模型中的 SomeObject 属性完成了此操作。但是当我在 SomeObject 的属性中更改某些内容时,它不会在 ViewModel 中调用 main SomeObject 的设置器,并且我无法从 SomeObject 类更改我的 IsEnabled 属性。

<controls:ExpandableSettingsPanel Header="REFUND" IsExpanded="{Binding RefundCustomerConfigurationEnabled, Mode=TwoWay}" ..>
    <StackPanel..>
        <CheckBox Content="Allowed within reversal period" IsChecked="{Binding RefundCustomerConfiguration.IsAllowedWithinReversalPeriod, Mode=TwoWay}"/>
        <TextBlock Style="{DynamicResource GrayTextBlockStyle}" Text="Minimal amount to be refunded"/>
        <WrapPanel>
            <TextBlock Text="€" .. />
            <controls:MementoTextBox Text="{Binding RefundCustomerConfiguration.MinimalAmountToBeRefunded, Mode=TwoWay, 
                NotifyOnValidationError=True, StringFormat=\{0:N2\}, 
                TargetNullValue='', 
                UpdateSourceTrigger=PropertyChanged, 
                ValidatesOnNotifyDataErrors=True, 
                ValidatesOnExceptions=True}"  ..>
                    <i:Interaction.Behaviors>
                        <behaviors:TextBoxInputRegExBehavior 
                            EmptyValue="0.00"
                            IgnoreSpace="True"
                            RegularExpression="^\d{1,2}(\.?\d{1,2})?$" 
                            MaxLength="6"
                            />
                    </i:Interaction.Behaviors>
            </controls:MementoTextBox>
        </WrapPanel>
        <TextBlock Style="{DynamicResource GrayTextBlockStyle}" Text="Minimal bank statement line age" ../>
        <controls:MementoTextBox Text="{Binding RefundCustomerConfiguration.MinimalBankStatementLineAge, 
            Mode=TwoWay,
            NotifyOnValidationError=True,
            ValidatesOnNotifyDataErrors=True,
            UpdateSourceTrigger=PropertyChanged,
            ValidatesOnExceptions=True }" ..>
            <i:Interaction.Behaviors>
                <behaviors:TextBoxInputRegExBehavior 
                        EmptyValue="0"
                        IgnoreSpace="True"
                        RegularExpression="^\d{1,3}$" 
                        MaxLength="3"
                        />
            </i:Interaction.Behaviors>
        </controls:MementoTextBox>
    </StackPanel>
</controls:ExpandableSettingsPanel>

这是我的 XAML 部分。

public RefundCustomerConfiguration RefundCustomerConfiguration
{
    get { return _refundCustomerConfiguration; }
    set
    {
        SetProperty(ref _refundCustomerConfiguration, value);
        OnPropertyChanged("RefundCustomerConfigurationEnabled");
    }
}

[Required(ErrorMessage = "This field is required")]
public bool IsAllowedWithinReversalPeriod
{
    get { return GetValue(() => IsAllowedWithinReversalPeriod); }
    set
    {
        SetPropertyValue(value);
    }
}

这是我的财产;首先 - 在 ViewModel 中。第二 - 在第一个属性类中。

【问题讨论】:

  • 这看起来像个谜题 :) 你应该分享一部分代码
  • 提供您的代码,以便我们找到错误
  • 听起来你需要学习使用的是ICommand接口,codeproject.com/Articles/238657/How-to-use-Commands-in-WPF
  • 您能否在“保存”按钮中包含 XAML?它应该绑定到 ViewModel 中的 SaveCommandICommand 接口为您提供了 CanExecute 检查,该检查自动用于启用/禁用绑定到命令的按钮。有一个例子 here 如果你想要的话。

标签: c# wpf mvvm inotifypropertychanged


【解决方案1】:

只需将按钮的可见性属性绑定到实现INotifyPropertyChanged接口的类中的Visibility方法,如下所示:

public Visibility btnVisible
{
   get
   {
      if(<your condition>)
         return Visibility.Visible;
      else
         return Visibility.Collapsed;
   }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-17
    • 2011-06-17
    • 1970-01-01
    • 1970-01-01
    • 2011-06-25
    相关资源
    最近更新 更多