简体   繁体   English

组合框选择更改时更新标签

[英]Updating a Label when Combobox Selection changes

I'm trying to update a label when the combo box selection is changed so that it presents a count from a query. 组合框选择更改时,我尝试更新标签,以便它显示查询中的计数。 Unless I specify suing a string the where clause, it wont let me use the value from the combobox.SelectedValue, it returns a zero. 除非我指定使用字符串where子句,否则它不会让我使用combobox.SelectedValue中的值,它将返回零。

where (o.ShipCountry == "USA")

And what I've been trying: 而我一直在尝试:

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;
    }

Here's a screen of the issue to help. 这是需要帮助的问题的屏幕。 The zero should be a count of all the orders to a certain country specified by the combo box. 零应该是组合框指定的到特定国家/地区的所有订单的计数。 在此处输入图片说明

Any help is much appreciated! 任何帮助深表感谢!

I actually realized my problem! 我实际上意识到了我的问题! The LINQ query I used to populate the ComboBox used an anonymous type to reference the column being selected. 我用来填充ComboBox的LINQ查询使用匿名类型来引用所选的列。 I named it "cc". 我将其命名为“ cc”。

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

Which meant the binding path was Binding Path=SelectedValue.cc 这意味着绑定路径是Binding Path=SelectedValue.cc

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

By removing the anonymous type I was able to access the SelectedValue of the combobox. 通过删除匿名类型,我可以访问组合框的SelectedValue。

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

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