public static BindResult BindControl <T>([NotNull] this Bindable <T> bindable, [NotNull] ComboBox cmb) { if (bindable == null) { throw new ArgumentNullException(nameof(bindable)); } if (cmb == null) { throw new ArgumentNullException(nameof(cmb)); } bool isSetting = false; Func <T> getter = () => (T)cmb.SelectedItem; Action <T> setter = b => { isSetting = true; try { if (!EqualityComparer <T> .Default.Equals((T)cmb.SelectedItem, b)) { cmb.SelectedItem = b; } } finally { isSetting = false; } }; EventHandler handler1 = (sender, args) => { if (cmb.DropDownStyle != ComboBoxStyle.DropDownList && !isSetting) { bindable.Value = getter(); } }; EventHandler handler2 = (sender, args) => { if (cmb.DropDownStyle == ComboBoxStyle.DropDownList && !isSetting) { bindable.Value = getter(); } }; var init = new BindResult( () => cmb.TextChanged += handler1, () => cmb.TextChanged -= handler1); init += new BindResult( () => cmb.SelectedIndexChanged += handler2, () => cmb.SelectedIndexChanged -= handler2); init += bindable.Bind(getter, setter); return(init); }
public static BindResult BindControl([NotNull] this Bindable <string> bindable, [NotNull] TextBox txt) { if (bindable == null) { throw new ArgumentNullException(nameof(bindable)); } if (txt == null) { throw new ArgumentNullException(nameof(txt)); } bool isSetting = false; Func <string> getter = () => txt.Text; Action <string> setter = b => { isSetting = true; try { int oldSelStart = txt.SelectionStart; int oldSelLength = txt.SelectionLength; var areEqual = txt.Text == b; if (txt.Text != b) { txt.Text = b; } if (areEqual) { txt.SelectionStart = oldSelStart; txt.SelectionLength = oldSelLength; } } finally { isSetting = false; } }; EventHandler handler = (sender, args) => { if (!isSetting) { bindable.Value = getter(); } }; var init = new BindResult( () => txt.TextChanged += handler, () => txt.TextChanged -= handler); init += bindable.Bind(getter, setter); return(init); }
public static BindResult Bind <TValue, TListItem>( [NotNull] this Bindable <TValue> bindable, [NotNull] ListBox lst, [NotNull] Func <TListItem, TValue> convertIn, [CanBeNull] Func <TValue, TListItem> convertOut = null ) { if (bindable == null) { throw new ArgumentNullException(nameof(bindable)); } if (lst == null) { throw new ArgumentNullException(nameof(lst)); } if (convertIn == null) { throw new ArgumentNullException(nameof(convertIn)); } bool isSetting = false; Func <TValue> getter = () => convertIn((TListItem)lst.SelectedItem); Action <TValue> setter = b => { isSetting = true; try { if (!EqualityComparer <TValue> .Default.Equals(convertIn((TListItem)lst.SelectedItem), b)) { lst.SelectedItem = convertOut != null ? (object)convertOut(b) : b; } } finally { isSetting = false; } }; EventHandler handler = (sender, args) => { if (!isSetting) { bindable.Value = getter(); } }; var init = new BindResult( () => lst.SelectedIndexChanged += handler, () => lst.SelectedIndexChanged -= handler); init += bindable.Bind(getter, setter); return(init); }
public static BindResult BindControl([NotNull] this Bindable <string> bindable, [NotNull] ComboBox cmb) { if (bindable == null) { throw new ArgumentNullException(nameof(bindable)); } if (cmb == null) { throw new ArgumentNullException(nameof(cmb)); } Func <string> getter = () => cmb.Text; Action <string> setter = b => { int oldSelStart = cmb.SelectionStart; int oldSelLength = cmb.SelectionLength; var areEqual = cmb.Text == b; cmb.Text = b; if (areEqual && cmb.DropDownStyle != ComboBoxStyle.DropDownList) { cmb.SelectionStart = oldSelStart; cmb.SelectionLength = oldSelLength; } }; EventHandler handler1 = (sender, args) => { if (cmb.DropDownStyle != ComboBoxStyle.DropDownList) { bindable.Value = getter(); } }; EventHandler handler2 = (sender, args) => { if (cmb.DropDownStyle == ComboBoxStyle.DropDownList) { bindable.Value = getter(); } }; var init = new BindResult( () => cmb.TextChanged += handler1, () => cmb.TextChanged -= handler1); init += new BindResult( () => cmb.SelectedIndexChanged += handler2, () => cmb.SelectedIndexChanged -= handler2); init += bindable.Bind(getter, setter); return(init); }
public static BindResult BindControl([NotNull] this Bindable <bool> bindable, [NotNull] CheckBox checkBox) { if (bindable == null) { throw new ArgumentNullException(nameof(bindable)); } if (checkBox == null) { throw new ArgumentNullException(nameof(checkBox)); } bool isSetting = false; Func <bool> getter = () => checkBox.Checked; Action <bool> setter = b => { isSetting = true; try { if (checkBox.Checked != b) { checkBox.Checked = b; } } finally { isSetting = false; } }; EventHandler handler = (sender, args) => { if (!isSetting) { bindable.Value = getter(); } }; var init = new BindResult( () => checkBox.CheckedChanged += handler, () => checkBox.CheckedChanged -= handler); init += bindable.Bind(getter, setter); return(init); }
public static BindResult BindControl <T>([NotNull] this Bindable <T> bindable, [NotNull] ListBox lst) { if (bindable == null) { throw new ArgumentNullException(nameof(bindable)); } if (lst == null) { throw new ArgumentNullException(nameof(lst)); } bool isSetting = false; Func <T> getter = () => (T)lst.SelectedItem; Action <T> setter = b => { isSetting = true; try { if (!EqualityComparer <T> .Default.Equals((T)lst.SelectedItem, b)) { lst.SelectedItem = b; } } finally { isSetting = false; } }; EventHandler handler = (sender, args) => { if (!isSetting) { bindable.Value = getter(); } }; var init = new BindResult( () => lst.SelectedIndexChanged += handler, () => lst.SelectedIndexChanged -= handler); init += bindable.Bind(getter, setter); return(init); }
public static BindResult BindControl([NotNull] this Bindable <string> bindable, [NotNull] Control control) { if (bindable == null) { throw new ArgumentNullException(nameof(bindable)); } if (control == null) { throw new ArgumentNullException(nameof(control)); } bool isSetting = false; Func <string> getter = () => control.Text; Action <string> setter = b => { isSetting = true; try { if (control.Text != b) { control.Text = b; } } finally { isSetting = false; } }; EventHandler handler = (sender, args) => { if (!isSetting) { bindable.Value = getter(); } }; var init = new BindResult( () => control.TextChanged += handler, () => control.TextChanged -= handler); init += bindable.Bind(getter, setter); return(init); }
public BindResult Initialize() { var mo = this.Model; var ma = this.ManagerModel; var mc = this.CertViewModel; BindResult init = BindResult.Null; // Primary initialization: init += new BindResult(() => mo.Registrations.Value = this.acme.GetRegistrations()); init += new BindResult(() => mo.Now.Value = DateTime.Now); init += mo.Date.BindExpression(() => mo.Now.Value.Date); init += mo.TosLink.BindExpression(() => this.acme.GetTos(mo.Registrations.Value, mo.Email.Value)); init += mo.Domain.BindOnChanged(strings => mo.Certificate.Value = ""); // Collections init += mo.Domains.BindExpression(() => this.acme.GetDomainsByEmail(mo.Registrations.Value, mo.Email.Value).OrderBy(x => x).ToArray()); init += mo.Certificates.BindExpression(() => this.Certifcates_value(mo.CurrentRegistration.Value, mo.CurrentIdentifier.Value)); init += mo.Challenge.BindOnChanged(this.Challenge_Changed); init += mo.CurrentAuthState.BindOnChanging(this.CurrentAuthState_Changing); // Complex relations: // These relations are built by using expressions. // Every bindable reference in the right hand side // have a changed event added automatically. init += mo.IsEmailValid.BindExpression(() => !string.IsNullOrWhiteSpace(mo.Email.Value)); init += mo.IsRegistrationCreated.BindExpression(() => mo.CurrentRegistration.Value != null); init += mo.IsDomainValid.BindExpression(() => !string.IsNullOrWhiteSpace(mo.Domain.Value) && mo.IsEmailValid.Value); init += mo.IsDomainCreated.BindExpression(() => mo.Domains.Value._S(v => v.Any(i => i == mo.Domain.Value)) ?? false); init += mo.IsChallengeValid.BindExpression(() => !string.IsNullOrWhiteSpace(mo.Challenge.Value) && mo.IsDomainValid.Value); init += mo.IsChallengeInitialized.BindExpression(() => mo.CurrentAuthState.Value._(v => v.Challenges._S(c => c.Any(x => x.Challenge != null))) ?? false); init += mo.IsTargetValid.BindExpression(() => !string.IsNullOrWhiteSpace(mo.Target.Value) && mo.IsChallengeInitialized.Value); init += mo.IsKeyValid.BindExpression(() => !string.IsNullOrWhiteSpace(mo.Key.Value) && mo.IsChallengeInitialized.Value); init += mo.IsSiteRootValid.BindExpression(() => !string.IsNullOrWhiteSpace(mo.SiteRoot.Value) && mo.IsChallengeInitialized.Value); init += mo.ChallengeHasFile.BindExpression(() => mo.IsTargetValid.Value && mo.IsKeyValid.Value && (mo.Challenge.Value != "http-01" || !string.IsNullOrWhiteSpace(mo.SiteRoot.Value))); init += mo.CurrentIdentifier.BindExpression(() => this.CurrentIdentifier_Value(mo.CurrentRegistration.Value, mo.Domain.Value)); init += mo.CurrentAuthState.BindExpression(() => this.CurrentAuthState_Value(mo.CurrentIdentifier.Value)); init += mo.CurrentChallenge.BindExpression(() => this.CurrentChallenge_Value(mo.CurrentAuthState.Value, mo.Challenge.Value)); init += mo.CurrentCertificate.BindExpression(() => this.CurrentCertificate_Value(mo.CurrentRegistration.Value, mo.Domain.Value, mo.Certificate.Value)); init += mo.X509Certificate.BindExpression(() => this.X509Certificate_Value(mo.CurrentCertificate.Value)); init += mo.Target.BindExpression(() => mo.CurrentChallenge.Value._(v => (v.Challenge as HttpChallenge)._(c => c.FileUrl)) ?? ""); init += mo.Key.BindExpression(() => mo.CurrentChallenge.Value._(v => v.Challenge._(c => (c.Answer as HttpChallengeAnswer)._(a => a.KeyAuthorization))) ?? ""); init += mo.FileRelativePath.BindExpression(() => mo.CurrentChallenge.Value._(v => (v.Challenge as HttpChallenge)._(c => c.FilePath)._(s => s.Replace('/', '\\'))) ?? ""); init += mo.FilePath.BindExpression(() => this.FilePath_Value(mo.ChallengeHasFile.Value, mo.SiteRoot.Value, mo.FileRelativePath.Value)); init += mo.Issuer.BindExpression(() => mo.CurrentCertificate.Value.With(v => v != null ? v.IssuerSerialNumber : "")); init += mo.CanRegister.BindExpression(() => this.CanRegister_Value(mo.Registrations.Value, mo.Email.Value, mo.IsEmailValid.Value)); init += mo.CanAcceptTos.BindExpression(() => this.CanAcceptTos_Value(mo.CurrentRegistration.Value)); init += mo.CanAddDomain.BindExpression(() => mo.CurrentRegistration.Value != null && mo.IsDomainValid.Value && !mo.IsDomainCreated.Value); init += mo.CanSaveChallenge.BindExpression(() => mo.ChallengeHasFile.Value); init += mo.CanCommitChallenge.BindExpression(() => this.CanCommitChallenge_Value(mo.SiteRoot.Value)); init += mo.CanTestChallenge.BindExpression(() => mo.IsTargetValid.Value && mo.IsKeyValid.Value); init += mo.CanUpdateStatus.BindExpression(() => mo.CurrentAuthState.Value != null); init += mo.CanValidateChallenge.BindExpression(() => false); // this value is changed after testing the challenge (it's not bound to anything) init += mo.CanCreateCertificate.BindExpression(() => mo.CurrentAuthState.Value.With(v => v != null && v.Status == "valid")); init += mo.CanSubmitCertificate.BindExpression(() => mo.CurrentCertificate.Value.With(v => v != null && v.CertificateRequest == null && string.IsNullOrWhiteSpace(v.IssuerSerialNumber))); init += mo.CanGetIssuerCertificate.BindExpression(() => mo.CurrentCertificate.Value.With(v => v != null && v.CertificateRequest != null && string.IsNullOrWhiteSpace(v.IssuerSerialNumber))); init += mo.CanSaveCertificate.BindExpression(() => mo.CurrentCertificate.Value.With(v => v != null && !string.IsNullOrWhiteSpace(v.IssuerSerialNumber)) && mo.CertificateType.Value != 0); var viewableCertTypes = new[] { CertType.CertificatePEM, CertType.CsrPEM, CertType.IssuerPEM, CertType.KeyPEM }; init += mo.CanShowCertificate.BindExpression(() => mo.CurrentCertificate.Value.With(v => v != null && !string.IsNullOrWhiteSpace(v.IssuerSerialNumber)) && Array.IndexOf(viewableCertTypes, mo.CertificateType.Value) >= 0); init += mo.IsPasswordEnabled.BindExpression(() => mo.CertificateType.Value == CertType.Pkcs12); init += mo.SsgTypes.BindExpression(() => _ssgTypes.Value.Select(SsgNameFromType).ToArray()); init += mo.CurrentSsg.BindExpression(() => this.CurrentSsg_Value(mo.SsgName.Value)); init += mo.ExpandedSavePath.BindExpression(() => this.ExpandedSavePath_Value(mo.SavePath.Value, mo.CertificateType.Value, mo.Certificate.Value)); init += mo.AvailableDomains.BindExpression(() => mo.CurrentRegistration.Value == null ? new string[0] : this.acme.GetIdentifiers(mo.CurrentRegistration.Value, null).Where(c => c.Authorization.Status == "valid").Select(c => c.Dns).ToArray()); // Certificate Viewer init += mc.CurrentCertificate.BindExpression(() => this.CurrentCertificate_Value(null, null, mc.Certificate.Value)); init += mc.Certificates.BindExpression(() => this.acme.GetCertificates(null, null).Where(c => c.IssuerSerialNumber != null).Select(c => c.Alias).ToArray()); init += mc.TextAssets.BindExpression(() => this.acme.GetTextAssets(mc.Certificate.Value, false)); init += mc.Base64Data.BindExpression(() => mc.TextAssets.Value == null ? null : mc.TextAssets.Value.GetAsset(mc.CertificateType.Value)); // when the key changes, the domain must be tested again init += mo.Key.BindOnChanged(s => mo.CanValidateChallenge.Value = false); init += mo.CurrentSsg.BindOnChanging(this.CurrentSsg_Changing); init += mo.CurrentSsg.BindOnChanged(this.CurrentSsg_Changed); return(init); }
public CreateAndBindResult(BindResult bindResult, Action removeTooltips, Action removeControls) { this.BindResult = bindResult; this.RemoveTooltips = removeTooltips; this.RemoveControls = removeControls; }
private BindResult BindModelsAndControls() { var mo = this.controller.Model; var ma = this.controller.ManagerModel; var mc = this.controller.CertViewModel; // Model bindings BindResult init = default(BindResult); init += mo.TosLink.BindOnChanged(s => this.DataTipFor(this.lnkTos, s, ">,v,>v,>v,>^,<v,<^")); // controller bindings init += this.controller.Initialize(); // Control bindings: // These are relations between the controls on the form // and the bindable objects. init += mo.Email.BindControl(this.cmbRegistration); init += mo.Domain.BindControl(this.cmbDomain); init += mo.Challenge.BindControl(this.cmbChallenge); init += mo.Target.BindControl(this.txtChallengeTarget); init += mo.Key.BindControl(this.txtChallengeKey); init += mo.SiteRoot.BindControl(this.txtSiteRoot); init += mo.Certificate.BindControl(this.cmbCertificate, s => s?.StartsWith("[new] ") == true ? s.Substring(6) : s); init += mo.Issuer.BindControl(this.txtIssuer); init += mo.CertificateType.BindControl(this.cmbCertificateType, StringToCertTypeEnum, CertTypeEnumToString); init += mo.Password.BindControl(this.txtPassword); init += mo.ShowPassword.BindControl(this.chkShowPassword); init += mo.GitUserName.BindControl(this.txtGitUserName); init += mo.GitPassword.BindControl(this.txtGitPassword); init += mo.SavePath.BindControl(this.txtSavePath); init += mo.AutoRegister.BindControl(this.chkAutoRegister); init += mo.AutoAcceptTos.BindControl(this.chkAutoAcceptTos); init += mo.AutoAddDomain.BindControl(this.chkAutoAddDomain); init += mo.AutoSaveChallenge.BindControl(this.chkAutoSaveChallenge); init += mo.AutoCommitChallenge.BindControl(this.chkAutoCommit); init += mo.AutoTestChallenge.BindControl(this.chkAutoTest); init += mo.AutoValidateChallenge.BindControl(this.chkAutoValidate); init += mo.AutoUpdateStatus.BindControl(this.chkAutoUpdateStatus); init += mo.AutoCreateCertificate.BindControl(this.chkAutoCreateCertificate); init += mo.AutoSubmitCertificate.BindControl(this.chkAutoSubmit); init += mo.AutoGetIssuerCertificate.BindControl(this.chkAutoGetIssuerCert); init += mo.AutoSaveOrShowCertificate.BindControl(this.chkAutoSaveOrShowCertificate); init += mo.CurrentRegistration.Bind(this.lstRegistrations, (RegistrationItem i) => i?.RegistrationInfo); init += mo.CurrentRegistration.BindControl(this.cmbRegistration, (RegistrationItem i) => i?.RegistrationInfo); init += mo.Domain.BindControl(this.lstDomains); init += ma.Challenge.BindControl(this.lstChallenges); // Cert view controls init += mc.Certificate.BindControl(this.cmbAllCerts); init += mc.Certificates.Bind(() => this.cmbAllCerts.Items.OfType <string>().ToArray()); init += BindHelper.BindExpression(() => this.cmbAllCerts.SetItems(mc.Certificates.Value)); init += mc.CertificateType.BindControl(this.cmbCertViewType, StringToCertTypeEnum, CertTypeEnumToString); init += mc.Base64Data.BindControl(this.txtCertBase64Data); // init += BindHelper.BindExpression(() => this.RetryToolTip(this.btnRegister, mo.AutoRegisterRetry.Value, mo.AutoRegisterTimer.Value)); init += BindHelper.BindExpression(() => this.RetryToolTip(this.btnAcceptTos, mo.AutoAcceptTosRetry.Value, mo.AutoAcceptTosTimer.Value)); init += BindHelper.BindExpression(() => this.RetryToolTip(this.btnAddDomain, mo.AutoAddDomainRetry.Value, mo.AutoAddDomainTimer.Value)); init += BindHelper.BindExpression(() => this.RetryToolTip(this.btnSaveChallenge, mo.AutoSaveChallengeRetry.Value, mo.AutoSaveChallengeTimer.Value)); init += BindHelper.BindExpression(() => this.RetryToolTip(this.btnCommitChallenge, mo.AutoCommitChallengeRetry.Value, mo.AutoCommitChallengeTimer.Value)); init += BindHelper.BindExpression(() => this.RetryToolTip(this.btnTestChallenge, mo.AutoTestChallengeRetry.Value, mo.AutoTestChallengeTimer.Value)); init += BindHelper.BindExpression(() => this.RetryToolTip(this.btnValidate, mo.AutoValidateChallengeRetry.Value, mo.AutoValidateChallengeTimer.Value)); init += BindHelper.BindExpression(() => this.RetryToolTip(this.btnUpdateStatus, mo.AutoUpdateStatusRetry.Value, mo.AutoUpdateStatusTimer.Value)); init += BindHelper.BindExpression(() => this.RetryToolTip(this.btnCreateCertificate, mo.AutoCreateCertificateRetry.Value, mo.AutoCreateCertificateTimer.Value)); init += BindHelper.BindExpression(() => this.RetryToolTip(this.btnSubmit, mo.AutoSubmitCertificateRetry.Value, mo.AutoSubmitCertificateTimer.Value)); init += BindHelper.BindExpression(() => this.RetryToolTip(this.btnGetIssuerCert, mo.AutoGetIssuerCertificateRetry.Value, mo.AutoGetIssuerCertificateTimer.Value)); init += BindHelper.BindExpression(() => this.RetryToolTip(this.btnSaveCertificate, mo.AutoSaveOrShowCertificateRetry.Value, mo.AutoSaveOrShowCertificateTimer.Value)); // Custom collection bindings: init += mo.Registrations.Bind( () => this.cmbRegistration.Items.AsArray((RegistrationItem x) => x.RegistrationInfo), v => this.cmbRegistration.SetItems(v.AsArray((RegistrationInfo x) => new RegistrationItem(x)))); init += mo.Registrations.Bind( () => this.lstRegistrations.Items.AsArray((RegistrationItem x) => x.RegistrationInfo), v => this.lstRegistrations.SetItems(v.AsArray((RegistrationInfo x) => new RegistrationItem(x)))); init += mo.Domains.Bind (() => this.cmbDomain.Items.AsArray((object o) => o.ToString()), v => this.cmbDomain.SetItems(v)); init += mo.Domains.Bind( () => this.lstDomains.Items.AsArray((object o) => o.ToString()), v => this.lstDomains.SetItems(v)); init += ma.Challenges.Bind( () => this.lstChallenges.Items.AsArray((object o) => o.ToString()), v => this.lstChallenges.SetItems(v)); init += ma.Challenges.BindOnChanged(fnames => { if (this.cmbChallenge.SelectedIndex < 0) { this.cmbChallenge.SelectedIndex = 0; } }); init += ma.Certificates.Bind( () => this.lstCertificates.Items.OfType <string>().ToArray(), v => this.lstCertificates.SetItems(v)); init += mo.Certificates.Bind(() => this.cmbCertificate.Items.OfType <string>().ToArray()); init += BindHelper.BindExpression( () => this.SetItemsOf_cmbCertificate(mo.Certificates.Value, mo.Domain.Value, mo.Date.Value)); init += mo.SsgName.BindControl(this.cmbSsg); init += mo.SsgTypes.Bind( () => this.cmbSsg.Items.OfType <string>().ToArray(), v => this.cmbSsg.SetItems(v)); init += mo.SsgTypes.BindOnChanged(fnames => { if (this.cmbSsg.SelectedIndex < 0) { this.cmbSsg.SelectedIndex = 0; } }); // Manual changed events: init += mo.CanRegister.BindOnChanged(v => this.btnRegister.Enabled = v); init += mo.CanAcceptTos.BindOnChanged(v => this.btnAcceptTos.Enabled = v); init += mo.IsRegistrationCreated.BindOnChanged(v => this.lnkTos.Enabled = v); init += mo.CanAddDomain.BindOnChanged(v => this.btnAddDomain.Enabled = v); init += mo.CanSaveChallenge.BindOnChanged(v => this.btnSaveChallenge.Enabled = v); init += mo.CanCommitChallenge.BindOnChanged(v => this.btnCommitChallenge.Enabled = v); init += mo.CanTestChallenge.BindOnChanged(v => this.btnTestChallenge.Enabled = v); init += mo.CanValidateChallenge.BindOnChanged(v => this.btnValidate.Enabled = v); init += mo.CanUpdateStatus.BindOnChanged(v => this.btnUpdateStatus.Enabled = v); init += mo.CanCreateCertificate.BindOnChanged(v => this.btnCreateCertificate.Enabled = v); init += mo.CanSubmitCertificate.BindOnChanged(v => this.btnSubmit.Enabled = v); init += mo.CanGetIssuerCertificate.BindOnChanged(v => this.btnGetIssuerCert.Enabled = v); init += mo.CanSaveCertificate.BindOnChanged(v => this.btnSaveCertificate.Enabled = v); init += mo.CanShowCertificate.BindOnChanged(v => this.btnShowCertificate.Enabled = v); init += mo.ShowPassword.BindOnChanged(b => this.txtPassword.UseSystemPasswordChar = !b); init += mo.IsPasswordEnabled.BindOnChanged(v => this.txtPassword.Enabled = this.chkShowPassword.Enabled = v); init += mo.Files.BindOnChanged(this.UpdateFiles); init += mo.CurrentAuthState.BindOnChanged(this.CurrentAuthState_Changed); init += mo.CurrentIdentifier.BindOnChanged(s => this.tableCertDomains.Hide()); init += mo.CurrentSsg.BindOnChanged(this.CurrentSsg_Changed); init += mo.X509Certificate.BindOnChanged(this.X509Certificate_Changed); ItemCheckEventHandler lstCertDomainsOnItemCheck = (s, a) => { if (this.lstCertDomains.Items[a.Index].ToString() == mo.Domain.Value) { a.NewValue = CheckState.Checked; } }; init += new BindResult( () => this.lstCertDomains.ItemCheck += lstCertDomainsOnItemCheck, () => this.lstCertDomains.ItemCheck -= lstCertDomainsOnItemCheck); return(init); }
public static BindResult BindControl( [NotNull] this Bindable <string> bindable, [NotNull] ComboBox cmb, [NotNull] Func <string, string> convertIn, [CanBeNull] Func <string, string> convertOut = null ) { if (bindable == null) { throw new ArgumentNullException(nameof(bindable)); } if (cmb == null) { throw new ArgumentNullException(nameof(cmb)); } bool isSetting = false; Func <string> getter = () => convertIn(cmb.Text); Action <string> setter = b => { int oldSelStart = cmb.SelectionStart; int oldSelLength = cmb.SelectionLength; var areEqual = convertIn(cmb.Text) == b; isSetting = true; try { cmb.Text = convertOut != null?convertOut(b) : b; } finally { isSetting = false; } if (areEqual && cmb.DropDownStyle != ComboBoxStyle.DropDownList) { cmb.SelectionStart = oldSelStart; cmb.SelectionLength = oldSelLength; } }; EventHandler handler1 = (sender, args) => { if (!isSetting) { if (cmb.DropDownStyle != ComboBoxStyle.DropDownList) { bindable.Value = getter(); } } }; EventHandler handler2 = (sender, args) => { if (!isSetting) { if (cmb.DropDownStyle == ComboBoxStyle.DropDownList) { bindable.Value = getter(); } } }; var init = new BindResult( () => cmb.TextChanged += handler1, () => cmb.TextChanged -= handler1); init += new BindResult( () => cmb.SelectedIndexChanged += handler2, () => cmb.SelectedIndexChanged -= handler2); init += bindable.Bind(getter, setter); return(init); }