ReactiveCommand与Int32属性上的CanExecute

时间:2017-02-08 23:13:58

标签: c# wpf reactiveui

我有下一个viewmodel,我使用canSubmit observable for SubmitOrder命令。 Quantity属性绑定到TextBox的text属性。当我在TextBox中输入一些文本时,它不会触发canSubmit observable并且仍然启用命令。 这是我的viewmodel:

public class CreateOrderViewModel : ReactiveObject
{
    private int _quantity;
    public int Quantity
    {
        get { return _quantity; }
        set { this.RaiseAndSetIfChanged(ref _quantity, value); }
    }

    private ISymbol _selectedSymbol;

    public ISymbol SelectedSymbol
    {
        get { return _selectedSymbol; }
        set { this.RaiseAndSetIfChanged(ref _selectedSymbol, value); }
    }

    private BuySell _side;

    public BuySell Side
    {
        get { return _side; }
        set { this.RaiseAndSetIfChanged(ref _side, value); }
    }

    public ReactiveCommand<Unit, IOrderProcessor> SubmitOrder { get; }

    public CreateOrderViewModel(IOrderService orderService)
    {
        var canSubmit = this.WhenAnyValue(v => v.SelectedSymbol, v => v.Quantity,
            (symbol, quantity) => symbol != null && quantity > 0);

        SubmitOrder = ReactiveCommand.Create(
            () =>   orderService.SubmitOrder(CreateOrderFromParameters()),
            canSubmit);
    }

    private Order CreateOrderFromParameters()
    {
        return new Order
        {
            Quantity = Quantity,
            Symbol = SelectedSymbol,
            Side = Side
        };
    }
}

我的XAML:

<TextBlock Grid.Row="0" Grid.Column="0"
           Text="{x:Static properties1:Resources.Quantity}"
           />
<TextBox Grid.Row="0" Grid.Column="1"
         Text="{Binding Quantity, UpdateSourceTrigger=PropertyChanged}"
         />

<TextBlock Grid.Row="0" Grid.Column="2"
           Text="{x:Static properties1:Resources.Symbol}"
           />
<ComboBox Grid.Row="0" Grid.Column="3"
          ItemsSource="{Binding Symbols}"
          DisplayMemberPath="Ticker"
          SelectedItem="{Binding SelectedSymbol}"
          />

<StackPanel Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="6"
            HorizontalAlignment="Left">
    <Button Content="{x:Static properties1:Resources.Submit}"
            Command="{Binding SubmitOrder}"
            />
</StackPanel>       

1 个答案:

答案 0 :(得分:0)

使用StringToNullableNumericConverter:

解决了我的问题
public class StringToNullableNumberConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return value;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var stringValue = value as string;

        if (stringValue == null) return value;

        if (targetType == typeof(int?))
        {
            int intValue;
            if (Int32.TryParse(stringValue, out intValue))
            {
                return intValue;
            }

            return null;
        }

        return value;
    }
}

使用以下viewmodel:

    private int? _quantity;
    public int? Quantity
    {
        get { return _quantity; }
        set
        {
            if (!value.HasValue || value <= 0)
            {
                SetError(nameof(Quantity), Resources.Properties.Resources.OrderQuantityMustBePositiveErrorMessage);
            }
            else
            {
                ClearErrors(nameof(Quantity));
                this.RaiseAndSetIfChanged(ref _quantity, value);
            }
        }
    }