我有以下视图模型:
public class TransportationUnit : ViewModelBase {
private string _TypeOfFuel;
private string _Model;
private string _Manufacturer;
private string _LicencePlate;
private Guid _Key = Guid.Empty;
public ICommand CmdAddTransportationUnit { get; set; }
public TransportationUnit() {
CmdAddTransportationUnit = new GalaSoft.MvvmLight.Command.RelayCommand( () => AddTransportationUnitDo(), () => AddTransportationUnitCan() );
}
/// <summary>manufacturer</summary>
public string Manufacturer {
get { return _Manufacturer; }
set {
if (_Manufacturer == value )
return;
RaisePropertyChanging( "Manufacturer" );
_Manufacturer = value;
RaisePropertyChanged( "Manufacturer" );
}
}
/* ommitted some equal properties */
public bool AddTransportationUnitCan() {
return !string.IsNullOrWhiteSpace( Model ) && !string.IsNullOrWhiteSpace( Manufacturer ) & !string.IsNullOrWhiteSpace( LicencePlate );
}
public async void AddTransportationUnitDo() {
await LogbookRepository.Instance.Add<TransportationUnit>( this );
}
}
我的文本框是这样绑定的:
<TextBox x:Name="CarManufacturerNameText" Width="400" HorizontalAlignment="Left" VerticalAlignment="Center" Grid.Row="0" Grid.Column="1" Text="{Binding Manufacturer,Mode=TwoWay}" />
我在 AppBar(底部)中的按钮是这样绑定的:
<Button Style="{StaticResource SaveAppBarButtonStyle}" AutomationProperties.Name="" x:Name="save" x:Uid="StandardSave" Command="{Binding CmdAddTransportationUnit}" />
我本来希望当方法AddTransportationUnitCan
评估为 false 时按钮被禁用,反之亦然。当所有文本框都填满时,它会一直被禁用,甚至在方法中设置的断点也只会在创建 RelayCommand 时触发一次。我已经测试了很长时间,但没有找到解决方案。还有其他人有这个问题吗?
编辑:当我在按钮中返回 true 时AddTransportationUnitCan
启用