【问题标题】:KeyValue Pair combobox .text after saved on database保存到数据库后的 KeyValue Pair 组合框 .text
【发布时间】:2017-04-10 21:39:20
【问题描述】:

我处理两个带有以下代码的组合框:

var Masters = new List<KeyValuePair<long, string>>();
using (var ctx = new GestioneCaricoEntities())
{
    var comti = ctx.ViewMasters.OrderBy(m => m.Lastname).ToList();
    foreach (var t in comti)
    {
        Masters.Add(new KeyValuePair<long, string>(t.Id, t.Lastname));
    }
}
cmbComteCaricazione.ItemsSource = Masters;

和

<ComboBox x:Name="cmbComteCaricazione" Margin="0,5,0,0" 
          DisplayMemberPath="Value" SelectedValuePath="Key" Width="110"  IsEditable="True" />

我保存在数据库中:

ts.IdComandanteCaricazione = (long?)cmbComteCaricazione.SelectedValue;

问题:一切都以正确的方式进行,db 中的表字段具有 idComandanteCaricazione 但是当我再次加载窗口时,我希望看到 cmbComteCaricazione.text 中的值。 我该怎么做?

【问题讨论】:

    标签: wpf entity-framework-6


    【解决方案1】:

    您需要从数据库中加载idComandanteCaricazione并将ComboBox的SelectedValue属性设置为它:

    public MainWindow()
    {
        InitializeComponent();
        this.Loaded += (s, e) =>
        {
            using (var ctx = new GestioneCaricoEntities())
            {
                ...
                cmbComteCaricazione.ItemsSource = Masters;
                cmbComteCaricazione.SelectedValue = ts.IdComandanteCaricazione;
            }
    
        };
    }
    

    【讨论】:

      猜你喜欢
      • 2014-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-05
      • 1970-01-01
      • 2018-02-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多