【问题标题】:Updating a Label when Combobox Selection changes组合框选择更改时更新标签
【发布时间】:2015-08-18 14:23:18
【问题描述】:

我正在尝试在更改组合框选择时更新标签,以便它显示来自查询的计数。除非我指定在 where 子句中使用字符串,否则它不会让我使用组合框中的值。SelectedValue,它会返回零。

where (o.ShipCountry == "USA")

以及我一直在尝试的:

private void cmbCountry_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        var ord = (from o in db.Orders
                   where (o.ShipCountry == cmbCountry.SelectedValuePath)
                   select o.ShipCountry).Count();

        lblOrders.Content = ord;
    }

这是一个帮助问题的屏幕。零应该是对组合框指定的某个国家/地区的所有订单的计数。

非常感谢任何帮助!

【问题讨论】:

  • ShipCountry == Belgium?我的意思是,它包含国家名称?
  • ShipCountry 可以是 Orders 表中的任何国家/地区。
  • 当你到达函数时cmbCountry.SelectedValuePath的值是什么
  • 你试过o.ShipCountry == cmbCountry.SelectedValue.ToString()吗?
  • 我在此之前尝试过。 @BugFinder 使用 cmbCountry.SelectedValuePath 实际上会起作用。我在用于填充组合框的查询中使用了匿名类型!如果我是正确的,价值路径应该是SelectedValue.cc? cc 因为匿名类型是 select new { cc = c.Country} 用于填充组合框的查询。

标签: c# xaml binding combobox


【解决方案1】:

我真的意识到了我的问题! 我用来填充 ComboBox 的 LINQ 查询使用匿名类型来引用所选择的列。我把它命名为“cc”。

var cmb = (from c in db.Customers
                   orderby c.Country descending
                   select new { cc = c.Country }).Distinct();

这意味着绑定路径是Binding Path=SelectedValue.cc

var cmb = (from c in db.Customers
               orderby c.Country descending
               select c.Country).Distinct();

通过删除匿名类型,我能够访问组合框的 SelectedValue。

【讨论】:

    猜你喜欢
    • 2018-04-03
    • 1970-01-01
    • 2017-07-18
    • 2019-08-08
    • 2016-06-25
    • 2010-12-20
    • 1970-01-01
    • 2021-05-03
    • 1970-01-01
    相关资源
    最近更新 更多