[英]INotifyPropertyChanged and Button.IsEnabled
我有 View 和它的 ViewModel。 在 View 中,我有一些 Panels 绑定到我的 ViewModel 中的某些属性;
例如,第一个属性是SomeObject
。 在那个面板中,我有一些文本框,它们绑定到SomeObject
类中的一些属性。 ViewModel 和类中的所有绑定都实现 INPC。
面板中的那个文本框以这种方式绑定到它在SomeObject
类中的属性: SomeObject.Property
。
在那个视图中,我有一个保存按钮。 每次在文本框中输入内容或在面板中更改内容时,我都需要更改按钮的IsEnabled
属性。
通过更改绑定到 SomeObject 的 setter 的按钮的IsEnabled
属性,我仅针对我的视图SomeObject
属性完成了此操作。 但是,当我更改SomeObject
属性中的SomeObject
它不会调用 ViewModel 中主SomeObject
的 setter,并且我无法从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 中。 第二 - 在第一类财产。
只需将按钮的可见性属性绑定到实现 INotifyPropertyChanged 接口的类中的 Visibility 方法,如下所示:
public Visibility btnVisible
{
get
{
if(<your condition>)
return Visibility.Visible;
else
return Visibility.Collapsed;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.