简体   繁体   English

所选项目未显示在WPF组合框中

[英]Selected item not showing in a WPF combobox

I'm creating a part of my window in code. 我正在用代码创建窗口的一部分。 For a combobox I do this: 对于组合框,我这样做:

ObservableCollection<ParamClassOption> options = new ObservableCollection<ParamClassOption>(
context.ParamClassOptions.Where(x => x.IDParamClass == val.CompTypeParam.IDParamClass));

ComboBox combobox = new ComboBox();
combobox.Name = "combobox" + val.CompTypeParam.ParameterName.Replace(" ", "");
combobox.ItemsSource = options;
combobox.SelectedValuePath = "IDParamClass";
combobox.DisplayMemberPath = "OptionName";
if (val.ParamClassOption != null)
{ 
  combobox.SelectedValue = val.ParamClassOption.IDParamClassOption; 
}
layoutitem.Content = combobox;

I'm able to select an item from the list and save it to the database. 我可以从列表中选择一个项目并将其保存到数据库中。 The problem that I have is to show the saved value again upon retrieving the values back from the database. 我的问题是从数据库取回值后再次显示保存的值。 Any idea why it's not showing? 知道为什么它不显示吗? val.ParamClassOption.IDParamClassOption in the second to last line above has the correct value when the record is retrieved to be displayed. 当检索到要显示的记录时,上面倒数第二行的val.ParamClassOption.IDParamClassOption具有正确的值。

i think you forgot to bind your selected value 我想你忘了你选择的值绑定

var binding = new Binding {Path = new PropertyPath("IDParamClassOption"), Mode = BindingMode.TwoWay, Source = val.ParamClassOption};
combobox.SetBinding(ComboBox.SelectedValueProperty, binding);

hope this helps 希望这可以帮助

Thanks for the help. 谢谢您的帮助。 I ended up using a completely different approach by adding the items to the combo-box one by one. 我最终使用了一种完全不同的方法,将这些项目一个接一个地添加到组合框中。 I then set the selected item to previously added value (using the Text property). 然后,将所选项目设置为先前添加的值(使用Text属性)。 Here is what my code looks like now: 这是我的代码现在的样子:

            if (controlType == "Combobox")
            {
                ComboBox combobox = new ComboBox();
                combobox.Name = "combobox" + val.CompTypeParam.ParameterName.Replace(" ", "");

                ObservableCollection<ParamClassOption> options = new ObservableCollection<ParamClassOption>(
                    context.ParamClassOptions.Where(x => x.IDParamClass == val.CompTypeParam.IDParamClass));
                combobox.Items.Clear(); 
                foreach (ParamClassOption option in options)
                {
                    ComboBoxItem item = new ComboBoxItem();
                    item.Content = option.OptionName;
                    combobox.Items.Add(item);
                }
                combobox.Text = val.ParamClassOption.OptionName;

                layoutitem.Content = combobox;
            }

Later when reading the value from the combobox to save to the database I did this: 稍后,当从组合框中读取值以保存到数据库时,我这样做:

ObservableCollection<ParamClassOption> option = new ObservableCollection<ParamClassOption>(
    context.ParamClassOptions.Where(o => o.IDParamClass == value.CompTypeParam.IDParamClass).Where(o => o.OptionName == combobox.Text));
value.IDParamClassOption = option[0].IDParamClassOption;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM