繁体   English   中英

使用带单选按钮的文本框进行按钮验证 WPF c#

[英]Button validation using Textbox with radiobutton WPF c#

当文本框具有特定类型/格式的文本输入( FilterString )时,我需要激活一个按钮,基于选择哪个单选按钮而不破坏 mvvm 模式。

基本上在按钮的 RelayCommand 实例的 CanExecute 方法中做类似的事情

public bool CanDoStuff(object param)
        {
            if (rb_1.IsChecked == true) {
                if (string.IsNullOrEmpty(FilterString)) {
                    return false;
                } else
                    return true;
            }
            
            else if (rb_2.IsChecked == true) {
                DateTime searchDate;
                if (DateTime.TryParseExact(FilterString, "dd-MM-yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out searchDate)) {
                    return true;
                } else
                    return false;
            }
            
            else if (rb_3.IsChecked == true) {
                Decimal k;
                if (Decimal.TryParse(FilterString, out k)) {
                    return true;
                } else
                    return false;
            }
            else
                return true;
        }

但是我无法在正确的 mvvm 设置中访问上面的单选按钮。 是否有不同的方法,例如使用IDataErrorInfo或其他方法,我该怎么做?

建议不要直接解释按钮状态。 而是根据其目的将它们抽象为有意义的 state 值。

  1. 您始终可以将每个RadioButton绑定到一个专用属性。
  2. 您可以使用IValueConverter将按钮的 state 转换为有意义的值,例如enum
  3. 当然,您可以使用ICommand将 state 作为有意义的值发送,例如enum
  4. 附加Click事件处理程序

解决方案 3) 很好,因为它消除了更复杂的Binding定义,例如 2) 使用IValueConverter引入的定义。 它使代码更具可读性。
因为分组的RadioButton是互斥的,所以您基本上需要为每个RadioButton组创建一个ICommand
您可以在此处找到常见的可重用ICommand实现RelayComandMicrosoft Docs:Relaying Command Logic

例如,如果您想使用RadioButton数组来允许用户选择一种货币,您可以首先实现一个enum来创建表示一种货币的常量。 然后为每个RadioButton使用enum值并使用ButtonBase.CommandParameter属性发送它:

货币.cs

public enum Currency
{
  Default = 0,
  Euro,
  BritishPound,
  JapaneseYen,
  UsDollar
}

MainViewModel.cs

class MainViewModel : INotifyPropertyChagned
{
  public Currency SelectedCurrency { get; private set; }

  // E.g., binding source for a TextBox
  // TODO::Raise INotifyPropertyChanged.PropertyChanged event
  public string Value { get; set; }

  public ICommand SelectCurrencyCommand { get; private set; }
  public ICommand SaveCommand { get; private set; }

  public MainViewModel()
  {
    this.SelectCurrencyCommand = new RelayCommand(ExecuteSelectCurrencyCommand, commandParameter => true);
    this.SaveCommand = new RelayCommand(ExecuteSaveCommand, CanExecuteSaveCommand);
  }

  private void ExecuteSelectCurrencyCommand(object commandParameter)
  {
    this.SelectedCurrency = (Currency)commandParameter;
  }
  
  // Your CanExecute handler, that depends on the state of the radio buttons.
  private bool CanExecuteSaveCommand(object commandParameter)
  {
    decimal decimalValue;
    switch (this.SelectedCurrency)
    {
      case Currency.Euro:
          return Decimal.TryParse(this.Value, out decimalValue);
      case Currency.BritishPound:
          return Decimal.TryParse(this.Value, out decimalValue);
      case Currency.JapaneseYen:
          break;
      case Currency.UsDollar:
          break;
      case Currency.Default:
          break;
      default:
        return false;
    }
  }
}

主窗口.xaml

<Window>
  <Window.DataContext>
    <MainViewModel />
  </Window.DataContext>

  <StackPanel>
    <RadioButton Command="{Binding SelectCurrencyCommand}"
                 CommandParameter="{x:Static Currency.Euro}" />
    <RadioButton Command="{Binding SelectCurrencyCommand}"
                 CommandParameter="{x:Static Currency.BritishPound}" />
    <RadioButton Command="{Binding SelectCurrencyCommand}"
                 CommandParameter="{x:Static Currency.JapaneseYen}" />

    <TextBox Text="{Binding Value}" />
    <Button Content="Save"
            Command="{Binding SaveCommand}" />
  </StackPanel>
</Window>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM