private void SetLengthValue() { SSWC.DataGridLengthUnitType unitType = (SSWC.DataGridLengthUnitType)_comboBox.SelectedItem; switch (unitType) { case SSWC.DataGridLengthUnitType.Auto: this.Value = SSWC.DataGridLength.Auto; break; case SSWC.DataGridLengthUnitType.SizeToCells: this.Value = SSWC.DataGridLength.SizeToCells; break; case SSWC.DataGridLengthUnitType.SizeToHeader: this.Value = SSWC.DataGridLength.SizeToHeader; break; case SSWC.DataGridLengthUnitType.Star: double starValue; if (double.TryParse(_textBox.Text, out starValue)) { // Store the last good star value _lastValidStarValue = starValue; this.Value = new SSWC.DataGridLength(starValue, SSWC.DataGridLengthUnitType.Star); } else { // The user entered something bad so revert to the last good value _textBox.Text = _lastValidStarValue.ToString(CultureInfo.InvariantCulture); // The Text binding does not update the Value in this case so set it explicitly this.Value = new SSWC.DataGridLength(_lastValidStarValue, SSWC.DataGridLengthUnitType.Star); } break; default: // Treat it as a Pixel length double pixels; if (double.TryParse(_textBox.Text, out pixels)) { // Store the last good pixel value _lastValidPixelWidth = pixels; this.Value = new SSWC.DataGridLength(pixels); } else { // The user entered something bad so revert to the last good value _textBox.Text = _lastValidPixelWidth.ToString(CultureInfo.InvariantCulture); // The Text binding does not update the Value in this case so set it explicitly this.Value = new SSWC.DataGridLength(_lastValidPixelWidth); } break; } }
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (!this._loadingValue) { SSWC.DataGridLengthUnitType unitType = (SSWC.DataGridLengthUnitType)_comboBox.SelectedItem; if (unitType == SSWC.DataGridLengthUnitType.Star) { _textBox.Text = _lastValidStarValue.ToString(CultureInfo.InvariantCulture); } else if (unitType == SSWC.DataGridLengthUnitType.Pixel) { _textBox.Text = _lastValidPixelWidth.ToString(CultureInfo.InvariantCulture); } SetLengthValue(); } }