Когда закрываю форму легко получаю значение SalectedValue ComboBox но перед ShowDialog формы если ставлю SelectedValue для ComboBox, то ComboBox не ставит его. ComboBox внутри TableLayoutPanel вместе с Label. те set SalectedValue не работает и Items = 0, но DataSource в конструкторе установлен
form.EnabledLabeledCheckComboxBox.CheckComboBox.SelectedValue = true;
form.ShowDialog();
MessageBox.Show(form.EnabledLabeledCheckComboxBox.CheckComboBox.SelectedValue.ToString()); // здесь то что я выберу
//////////////////////////////////////////////////////////////////
public class BoolKeyStringValue : BaseVM {
#region Key
private bool key;
public bool Key {
get => this.key;
set {
this.key = value;
OnPropertyChanged("Key");
}
}
#endregion
#region Value
private string value;
public string Value {
get => this.value;
set {
this.value = value;
OnPropertyChanged("Value");
}
}
#endregion
public BoolKeyStringValue(bool key, string value) {
this.Key = key;
this.Value = value;
}
}
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Test {
public class LabeledCheckComboBox : TableLayoutPanel {
#region Properties
#region Label
private Label label;
public Label Label {
get => this.label;
set => this.label = value;
}
#endregion
#region CheckComboBox
private ComboBox checkComboBox;
public ComboBox CheckComboBox {
get => this.checkComboBox;
set => this.checkComboBox = value;
}
#endregion
#region Bools
private List<BoolKeyStringValue> bools;
public List<BoolKeyStringValue> Bools {
get => this.bools;
set => this.bools = value;
}
#endregion
#endregion
#region Constructors
public LabeledCheckComboBox() {
#region Label
this.Label = new Label();
this.Label.Dock = DockStyle.Bottom;
this.Label.Margin = new Padding(0);
#endregion
#region Bools
this.Bools = new List<BoolKeyStringValue>();
this.Bools.Add(new BoolKeyStringValue(false, "No"));
this.Bools.Add(new BoolKeyStringValue(true, "Yes"));
#endregion
#region Init
this.Dock = DockStyle.Top;
this.Margin = new Padding(0, 1, 0, 0);
this.RowCount = 2;
this.ColumnCount = 1;
this.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100));
this.Controls.Add(this.Label, 0, 0);
this.CheckComboBox = new ComboBox();
this.CheckComboBox.Items.AddRange(this.Bools.ToArray());
this.CheckComboBox.DataSource = this.Bools;
this.CheckComboBox.DisplayMember = "Value";
this.CheckComboBox.ValueMember = "Key";
this.CheckComboBox.Dock = DockStyle.Top;
this.CheckComboBox.Margin = new Padding(3, 0, 0, 0);
this.CheckComboBox.FlatStyle = FlatStyle.Flat;
this.CheckComboBox.DropDownStyle = ComboBoxStyle.DropDownList;
this.CheckComboBox.Font = new Font("Microsoft Sans Serif", 10, FontStyle.Regular);
this.Controls.Add(this.CheckComboBox, 0, 1);
#endregion
}
#endregion
}
}
BaseVM
реализует интерфейсINotifyPropertyChanged
?