我有以下ViewModel:
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时触发一次。我已经测试了很长时间,但还没有找到解决方案。其他人有这个问题吗?
修改:当我在AddTransportationUnitCan
中返回true时按钮已启用
答案 0 :(得分:2)
尝试将RaiseCanExecuteChanged添加到您的属性设置器中。这将告诉您的RelayCommand重新评估CanExecute。
public string Manufacturer {
get { return _Manufacturer; }
set {
_Manufacturer = value;
RaisePropertyChanged( "Manufacturer" );
CmdAddTransportationUnit.RaiseCanExecuteChanged();
}
}