我有菜单条,每个项目都有它的组合框

本文关键字:组合 项目 菜单 | 更新日期: 2023-09-27 17:59:49

我有菜单条,每个项目都有自己的组合框。在运行时,我希望从sql数据库表中加载combo。我尝试了dataadapter,但在运行时所有组合框都是空白的。请在这个话题上帮我。

我有菜单条,每个项目都有它的组合框

哼。。。如果你使用的是支持数据绑定的东西,也许你可以使用类似MVVM的模式。

1.为数据库表建立一个模型,您需要一些东西来存储状态。

public class CheckState
{
    public bool IsChecked { get; set; }
    public CheckState(bool isChecked)
    {
        this.IsChecked = isChecked;
    }
}

public class Model
{
    public bool[] CheckStates { get; set; }
}

2.ViewModel应该是这样的。

public class ViewModel : INotifyPropertyChanged //this is important
{
    public event PropertyChangedEventHandler PropertyChanged;
    public void RaisePropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    private Model model;
    public List<CheckState> checkStates = new List<CheckState>();
    public List<CheckState> CheckStates
    {
        get { return checkStates; }
        set
        {
            checkStates = value;
            RaisePropertyChanged("CheckStates"); //And this is important
        }
    }
    public ViewModel()
    {
        InitializeModel();
    }
    private void InitializeModel()
    {
        //here to retrieve data from your database
        model = new Model
                    {
                        CheckStates = new[] { true, false, true, false }
                    };
        foreach(var stateItem in model.CheckStates)
        {
            this.checkStates.Add(new CheckState(stateItem));
        }
    }
}

3.并在初始化ViewModel时填充Model的属性。

    public void InitilizeModelData()
    {
        // In gernal, the data comes from database
        var model = new CaculatorModel()
        {
            IsChecked = True,
        };
        IsChecked = model.IsChecked;
    }

4.设置用户界面。将checkBox的isChecked属性绑定到ViewModel的isChecked属性。

public partial class MainWindow : Window
{
    public MainWindow()
    {
        DataContext = new ViewModel(); //And this is also important
        InitializeComponent();
    }
}

和XML:(请尝试弄清楚父亲和孩子之间的关系)

    <Menu ItemsSource="{Binding CheckStates}">
        <Menu.ItemTemplate>
            <DataTemplate>
                <CheckBox IsChecked="{Binding IsChecked}"></CheckBox>
            </DataTemplate>
        </Menu.ItemTemplate>
    </Menu>

好吧,我说得太多了

我想你指示了一些与我的问题无关的东西。必须从sql数据库调用数据。我附上表格的形状。目标组合框名称为cmbCement,上一个菜单为tsmCement。我不确定在属性中添加项目是否适合我的表单目标。

public void FrmInterface_Load(对象发送方,EventArgs e)

    {
        SqlConnection con = new SqlConnection("server = .''SQLEXPRESS ; DataBase = Badban; Integrated Security                          = true");
       con.Open();

        SqlDataAdapter da = new SqlDataAdapter("SelectCement", con);
        SqlCommand com=new SqlCommand("SelectCement",con);
        com.CommandType = CommandType.StoredProcedure;
        da.SelectCommand.CommandType = CommandType.StoredProcedure;
        DataSet dt = new DataSet();
        da.Fill(dt);
        for (int i = 0; i < dt.Tables[0].Rows.Count; i++)
        {
            cmbCement.DroppedDown.Add(dt.Tables[0].Rows);
            cmbCement.ComboBox.DataSource = dt.Tables[0];
            cmbCement.ComboBox.ValueMember = "ID";
            cmbCement.ComboBox.Text = "Name";   
            con.Close();
        }