确保我的Textbox和ComboBox选择了值

时间:2018-01-04 22:01:21

标签: c# wpf xaml mvvm

我希望能够在练习中选择所需的重复次数(组合框下拉菜单),并让用户输入使用过的重量(文本框)并计算该练习的最大重量。

enter image description here

所以我的问题是;当我从我的组合框中选择一个所需的数字时,就像它没有选择"选择"当我调试时,如果这是有道理的。我坦率地说,我不知道在哪一方面我会制作实际的"计算器"?

所以我做了一些课程和观点:

RepMaxCalcDataStorage

public class RepMaxCalcDataStorage : ViewModelBase
{
    private int _repRange;
    private double _weightUsed;

    public int CalculateRepRange 
    { 
        get { return _repRange; } 
        set { value = _repRange; } 
    }

    public double CalculateWeightUsed 
    { 
        get { return _weightUsed; } 
        set { value = _weightUsed; } 
    }
}

ViewModelBase

public abstract class ViewModelBase : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this,
        new PropertyChangedEventArgs(propertyName));
    }
}

OneRepMaxCalcViewModel

public class OneRepMaxCalcViewModel : ViewModelBase
{
    private List<double> _repRange;
    private RepMaxCalcDataStorage _dataStorage;
    private int _ItemIsSeleceted;

    public OneRepMaxCalcViewModel ()
    {
        _repRange = new List<double>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
        _dataStorage = new RepMaxCalcDataStorage();
    }

    public List<double> GetRepRange { get { return _repRange; } set { value = _repRange; } }
    public RepMaxCalcDataStorage DataStorage { get { return _dataStorage; } set { value = _dataStorage; } }
    public int SelectedRep { get { return _ItemIsSeleceted;  }
        set
        {
            value = _dataStorage.CalculateRepRange ;
            if(_ItemIsSeleceted != 0)
            {
                _dataStorage.CalculateRepRange = _ItemIsSeleceted;
            }
            OnPropertyChanged(nameof(RepMaxCalcDataStorage));
        }
    }

    public double WeightUsed 
    { 
        get { return _dataStorage.CalculateWeightUsed; } 
        set { value = _dataStorage.CalculateWeightUsed; } 
    }
}

RepMaxCalculator

public class _1RepMaxCalcViewModel : ViewModelBase
{
    private List<double> _repRange;
    private double _weightLifted;
    private double _calculatedResults;
    private RepMaxCalcDataStorage _dataStorage;

    public _1RepMaxCalcViewModel(double CalculatedResults, double WeightLifted)
    {
        _repRange = new List<double>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
        _weightLifted = WeightLifted;
        _calculatedResults = CalculatedResults;
        _dataStorage = new RepMaxCalcDataStorage();
    }

    public List<double> GetRepRange 
    { 
        get { return _repRange; } 
        set { value = _repRange; } 
    }

    public RepMaxCalcDataStorage DataStorage 
    { 
        get { return _dataStorage; } 
        set { value = _dataStorage; } 
    }

    public double Calculate1RepMax 
    { 
        get { return _calculatedResults; } 
    }

    public double TotalWeightLifted 
    { 
        get { return _weightLifted; } 
        set { value = _weightLifted; } 
    }
}

1RepMaxCalculatorPage

<StackPanel Orientation="Vertical" Margin="543,313,463,588">
        <TextBlock HorizontalAlignment="Left" Foreground="White" Margin="10" TextWrapping="Wrap" Text="Select Rep Range:" VerticalAlignment="Top" Height="22"/>
        <ComboBox HorizontalAlignment="Left" Background="DimGray" ItemsSource="{Binding GetRepRange}" SelectedItem="{Binding SelectedRep, Mode=TwoWay}" Width="180"  />
    </StackPanel>
 <StackPanel Orientation="Vertical" Margin="724,313,1011,588">
        <TextBlock HorizontalAlignment="Left" Foreground="White" Margin="10" TextWrapping="Wrap" Text="Weight used:" VerticalAlignment="Top" Height="22"/>
        <TextBox HorizontalAlignment="Left" Background="#e5e5e5" TextWrapping="Wrap" VerticalAlignment="Top" Width="180"/>
    </StackPanel>
  <StackPanel Orientation="Vertical" Margin="961,313,774,588">
        <TextBlock HorizontalAlignment="Left" Foreground="White" Margin="10" TextWrapping="Wrap" Text="Your 1 Rep M:" VerticalAlignment="Top" Height="22"/>
        <TextBox HorizontalAlignment="Left" Background="#e5e5e5" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="180" IsReadOnly="True"/>
    </StackPanel>

解:

我的类和xaml看起来是什么样的,现在我找到了解决方案:

RepMaxCalcDataStorage

  public class RepMaxCalcDataStorage : ViewModelBase
{
    private int _repRange;
    private double _weightUsed;

    public RepMaxCalcDataStorage()
    {

    }

    public int CalculateRepRange
    {
        get { return _repRange; }
        set { _repRange = value; OnPropertyChanged(); }
    }

    public double CalculateWeightUsed
    {
        get { return _weightUsed; }
        set { _weightUsed = value; OnPropertyChanged(); }
    }

}

ViewModelBase

与以前相同。

OneRepMaxCalcViewModel

public class OneRepMaxCalcViewModel : ViewModelBase
{
    private List<double> _repRange;
    private RepMaxCalcDataStorage _dataStorage;
    private RelayCommand _relayCommand;
    private double _calculationResults;

    public OneRepMaxCalcViewModel ()
    {
        _repRange = new List<double>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
        _dataStorage = new RepMaxCalcDataStorage();
        _relayCommand = new RelayCommand(CalculateMax);
    }

    public List<double> GetRepRange
    {
        get { return _repRange; }
        set { _repRange = value; }
    }

    public RepMaxCalcDataStorage DataStorage
    {
        get { return _dataStorage; }
        set { _dataStorage = value ; }
    }

    public int SelectedRep
    {
        get { return _dataStorage.CalculateRepRange; }
        set
        {
            _dataStorage.CalculateRepRange = value ;
            OnPropertyChanged();
        }
    }

    public RelayCommand GetCalculationResults
    {
        get { return _relayCommand; }
        set { _relayCommand = value; OnPropertyChanged(); }
    }

    public double GetResults
    {
        get { return _calculationResults; }
        set { _calculationResults = value; OnPropertyChanged(); }
    }

    public void CalculateMax()
    {
        _calculationResults = (DataStorage.CalculateWeightUsed * DataStorage.CalculateRepRange * 0.033) + DataStorage.CalculateWeightUsed;
        OnPropertyChanged(nameof(GetResults));
    }
}

RepMaxCalculator

删除,因为它是重复的代码。

1RepMaxCalculatorPage

Combobox&#34;选择Rep范围&#34;绑定:

ItemsSource="{Binding GetRepRange}" SelectedItem="{Binding SelectedRep, Mode=TwoWay}"

TextBox&#34;使用重量&#34;绑定:

Text="{Binding DataStorage.CalculateWeightUsed, Mode=TwoWay}"

按钮&#34;计算&#34;绑定:

Command="{Binding GetCalculationResults}"

文本框&#34;您的1个代表M&#34;

Text="{Binding GetResults}"

1 个答案:

答案 0 :(得分:0)

在你的属性的setter中,你正在放

value = _repRange;

其中_repRange用作示例,因为此处显示的所有设置器都是相同的。

这是向后的,你希望变量接收值。

_repRange = value;

同样在OneRepMaxCalcViewModel类中,将SelectedRep setter更改为类似的内容。

public int SelectedRep { get { return _ItemIsSelected;  }
    set
    {
        _dataStorage.CalculateRepRange = value;
        _ItemIsSelected = value;
        OnPropertyChanged(nameof(RepMaxCalcDataStorage));
    }
}

这使得_ItemIsSelected始终更新为当前选定的项目。除此之外,您还需要在加载表单时将comboBox的选择设置为有效值,或者将_ItemIsSelected初始化为某个值(例如Int32.MinValue),这表示没有选择。这是因为用户在使用变量时没有选择任何内容。