public CalculatorViewModel() : base(false) { _result = CalculatedProperty<string>.Create(Operand1, Operand2, SelectedOperator, (operand1, operand2, selectedOperator) => { if (selectedOperator.Value == null) { return null; } Func<double?, double?, double?> function; switch (selectedOperator.Value.Value) { case Operator.Add: function = (x, y) => x + y; break; case Operator.Subtract: function = (x, y) => x - y; break; case Operator.Multiply: function = (x, y) => x*y; break; case Operator.Divide: function = (x, y) => x/y; break; default: throw new NotSupportedException( "Unknown enum value " + selectedOperator.Value.Value + "."); } return function(FrameworkUtility.DoubleTryParse(operand1.Value), FrameworkUtility.DoubleTryParse(operand2.Value)).SafeToString(); }); }
public WindowTimer(Action callback, int milliseconds, bool autoReset) { _callback = callback; _milliseconds = milliseconds; _autoReset = autoReset; _isRunning = CalculatedProperty<bool>.Create(_timerId, timerId => timerId.Value != null); }
public CalculatorPageViewModel(CalculatorsAndStopwatchApplicationViewModel applicationViewModel) { _applicationViewModel = applicationViewModel; _navigationViewModel = new NavigationViewModel(_applicationViewModel); _isCalculator2 = new ObservableProperty<bool>(); _calculatorViewModel = new ObservableProperty<CalculatorViewModel>(_calculatorViewModel1); _calculatorText = CalculatedProperty<string>.Create(_isCalculator2, isCalculator2 => "Calculator " + (isCalculator2.Value ? "2" : "1")); }
public RemoteCalculatorViewModel() : base(true) { _result = AsyncCalculatedProperty<string>.Create(Operand1, Operand2, SelectedOperator, (operand1, operand2, selectedOperator, setValue) => { if (selectedOperator.Value == null) { setValue(null); } else { //CustomObject c = new CustomObject(); //c.Add = null; //c.Property1 = "asdfksdhkdfh"; //c.Property2 = 29; //CustomObject c3 = new CustomObject(); //c3.Property1 = null; //c3.Property2 = 8; //c3.Property5 = SomeEnum.Value1; //c.Property3.Add(c3); //CustomObject2 c2 = new CustomObject2(); //c2.Property1 = "341231"; //c2.Property2 = null; //c2.Property3 = SomeEnum.Value2; //c.Property4 = c2; //c.Property5 = SomeEnum.Value1; //CustomObject cc = new CustomObject(); //cc.Property5 = SomeEnum.Value3; //CalculatorClient t = new CalculatorClient(); //t.Url = "http://localhost/CsJsCalculatorsAndStopwatchExampleServices/CalculatorService.svc"; //t.TestMethod(7.2, 3, false, "something value", c, new List<string> { "a", "b", "c" }, null, new List<int?> { 1, null, 3 }, new List<CustomObject> { c, cc }, o => FrameworkUtility.Debugger(), (request, textStatus, error) => FrameworkUtility.Debugger()); CalculatorClient calculatorClient = WebServiceClientFactory.Instance.CreateCalculatorClient(); Action<double?, double?, bool, Action<double?>, Action<jQueryXmlHttpRequest, string, string>> method; switch (selectedOperator.Value.Value) { case Operator.Add: method = calculatorClient.Add; break; case Operator.Subtract: method = calculatorClient.Subtract; break; case Operator.Multiply: method = calculatorClient.Multiply; break; case Operator.Divide: method = calculatorClient.Divide; break; default: throw UnhandledEnumValueExceptionFactory.Create(selectedOperator.Value.Value); } method(FrameworkUtility.DoubleTryParse(operand1.Value), FrameworkUtility.DoubleTryParse(operand2.Value), SimulateLatency.Value, o => setValue(o.SafeToString()), (request, textStatus, error) => setValue("Error: " + error)); } }); _resultToDisplay = CalculatedProperty<string>.Create(_result, _result.IsCalculating, (result, isCalculating) => isCalculating.Value ? "Calculating..." : result.Value); }
public StopwatchViewModel() { _viewModes = new ObservableCollection<ViewMode>(new[] { ViewMode.Milliseconds, ViewMode.Seconds }); _selectedViewMode = new ObservableProperty<ViewMode?>(ViewMode.Seconds); _isActive = new ObservableProperty<bool>(true); _isActive.Changed += (sender, args) => UpdateTimerState(); _elapsed = new ObservableProperty<TimeSpan>(); _elapsedString = CalculatedProperty<string>.Create(_elapsed, _selectedViewMode, (elapsed, selectedViewMode) => GetTimeSpanString(elapsed.Value, selectedViewMode.Value)); _isRunning = new ObservableProperty<bool>(); _isRunning.Changed += (sender, args) => UpdateTimerState(); _allowStop = CalculatedProperty<bool>.Create(_isRunning, isRunning => isRunning.Value); _allowStart = CalculatedProperty<bool>.Create(_isRunning, isRunning => !isRunning.Value); _timer = TimerFactory.Instance.CreateTimer(OnTimerElapsed, 27, true); }
internal MainWindowViewModel(Settings settings) { this.readOnlyEvents = new ReadOnlyObservableCollection <Event>(this.events); this.consumerConnection = new ConnectionViewModel(this); this.providerConnection = new ConnectionViewModel(this); this.canEditSettings = CalculatedProperty.Create( this.GetProperty(o => o.IsStarted), this.GetProperty(o => o.IsStopped), (isStarted, isStopped) => !isStarted && isStopped, this.GetProperty(o => o.CanEditSettings)); #region CalculatedProperty2 this.canStart = CalculatedProperty.Create( this.GetProperty(o => o.IsStarted), this.GetProperty(o => o.IsStopped), this.GetProperty(o => o.ListeningPort), this.GetProperty(o => o.ProviderPort), this.GetProperty(o => o.LogFolder), (isStarted, isStopped, lp, pp, lf) => !isStarted && isStopped && string.IsNullOrEmpty(ValidatePort(lp) + ValidatePort(pp) + ValidateFolder(lf)), this.GetProperty(o => o.CanStart)); #endregion this.canStop = CalculatedProperty.Create( this.GetProperty(o => o.IsStopped), s => !s, this.GetProperty(o => o.CanStop)); this.settings = settings; #region TwoWayBinding TwoWayBinding.Create( this.settings.GetProperty(o => o.ListeningPort), this.GetProperty(o => o.ListeningPort)); TwoWayBinding.Create( this.settings.GetProperty(o => o.ProviderHostName), this.GetProperty(o => o.ProviderHostName)); TwoWayBinding.Create( this.settings.GetProperty(o => o.ProviderPort), this.GetProperty(o => o.ProviderPort)); TwoWayBinding.Create( this.settings.GetProperty(o => o.LogFolder), this.GetProperty(o => o.LogFolder)); TwoWayBinding.Create( this.settings.GetProperty(o => o.AutoScrollToMostRecentEvent), a => a, this.GetProperty(o => o.AutoScrollToMostRecentEvent), a => a.GetValueOrDefault()); #endregion this.AddValidationRule(this.GetProperty(o => o.ProviderPort), ValidatePort); this.AddValidationRule(this.GetProperty(o => o.ListeningPort), ValidatePort); this.AddValidationRule(this.GetProperty(o => o.LogFolder), ValidateFolder); }
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// internal ConnectionViewModel(MainWindowViewModel parent) { this.parent = parent; this.connectionCount = CalculatedProperty.Create( this.parent.GetProperty(o => o.IsStopped), this.GetProperty(o => o.ConnectionCountCore), (s, c) => GetCount(!s, c), this.GetProperty(o => o.ConnectionCount)); this.bytesReceived = CalculatedProperty.Create( this.GetProperty(o => o.Client), this.GetProperty(o => o.BytesReceivedCore), (c, r) => GetCount(c != null, r), this.GetProperty(o => o.BytesReceived)); this.secondsSinceLastReceived = CalculatedProperty.Create( this.GetProperty(o => o.Client), this.parent.GetProperty(o => o.Now), this.GetProperty(o => o.LastReceived), (c, n, r) => c == null ? string.Empty : ((long)(n - r).TotalSeconds).ToString(CultureInfo.InvariantCulture), this.GetProperty(o => o.SecondsSinceLastReceived)); }
static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); CustomPropertyTypeDescriptor.Register(typeof(OrderLine), CalculatedProperty.Create("Total", (OrderLine source) => source.Quantity * source.Price) ); var form = new Form(); var dg = new DataGridView { Dock = DockStyle.Fill, Parent = form }; dg.DataSource = Enumerable.Range(1, 10).Select(n => new OrderLine { ItemNo = "Item#" + n, Quantity = n, Price = 10 * n }).ToList(); Application.Run(form); }
protected CalculatorViewModelBase(bool supportsAsync) { _updateInRealTimeItems = new ObservableCollection<bool>(new[] { true, false }); _updateInRealTimeSelection = new ObservableProperty<bool?>(true); _updateInRealTime = CalculatedProperty<bool>.Create(_updateInRealTimeSelection, updateInRealTimeSelection => updateInRealTimeSelection.Value.HasValue && updateInRealTimeSelection.Value.Value); _supportsAsync = new ReadOnlyProperty<bool>(supportsAsync); _simulateLatencyItems = new ObservableCollection<bool>(new[] { true, false }); _simulateLatencySelection = new ObservableProperty<bool?>(true); _simulateLatency = CalculatedProperty<bool>.Create(_simulateLatencySelection, simulateLatencySelection => simulateLatencySelection.Value.HasValue && simulateLatencySelection.Value.Value); _operators = new ObservableCollection<Operator>(new[] { Operator.Add, Operator.Subtract, Operator.Multiply, Operator.Divide }); _operand1 = new ObservableProperty<string>(); _selectedOperator = new ObservableProperty<Operator?>(); _selectedOperator.Value = Operator.Add; _selectedOperatorString = CalculatedProperty<string>.Create(_selectedOperator, selectedOperator => { if (selectedOperator.Value == null) { return null; } switch (selectedOperator.Value.Value) { case Operator.Add: return "+"; case Operator.Subtract: return "-"; case Operator.Multiply: return "*"; case Operator.Divide: return "/"; default: throw new NotSupportedException("Unknown enum value " + selectedOperator.Value.Value + "."); } }); _operand2 = new ObservableProperty<string>(); }