public class ViewModel : INotifyPropertyChanged
{
public double A
{
get => _a;
set
{
if (value.Equals(_a)) return;
_a = value;
OnPropertyChanged();
}
}
public double B
{
get => _b;
set
{
if (value.Equals(_b)) return;
_b = value;
OnPropertyChanged();
}
}
public double C
{
get => _c;
set
{
if (value.Equals(_c)) return;
_c = value;
OnPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
private double _a = 1;
private double _b = 2;
private double _c;
[NotifyPropertyChangedInvocator]
private void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
这时在界面如果需要创建一个 TextBlock 绑定三个值 A + B + C 就可以通过下面的方法
="!IsChecked" IsChecked="{c:Binding !IsChecked}"/>
="{c:Binding ‘IsChecked and IsFull‘}"/> {‘and‘ is equvalent of ‘&&‘}
="{c:Binding ‘!IsChecked or (A > B)‘}"/> {‘or‘ is equvalent of ‘||‘, but you can leave ‘||‘}
="{c:Binding ‘(A == 1) and (B less= 5)‘}"/> {‘less=‘ is equvalent of ‘}
="{c:Binding (IsChecked || !IsFull)}"/>
因为在 xaml 不能使用 &&|| 所以需要使用 and or ‘less=’ 替换
另外对于 : 之前需要添加空格,如下面代码
="{c:Binding ‘(A == 2)?IsChecked : IsFull}"/> !-- right -->
="{c:Binding ‘(A == 2)?IsChecked :!IsFull}"/> !-- right -->
="{c:Binding ‘(A == 2) ? IsChecked :4 + IsFull}"/> !-- right -->