【问题标题】:WPF cascading comboboxes with LINQ带有 LINQ 的 WPF 级联组合框
【发布时间】:2017-02-01 07:37:46
【问题描述】:

我在自定义控件上确实有许多组合框,如下所示

<Label Grid.Row="12" Grid.Column="0" Name="lblCombobox1">
    Select value from Combobox1
</Label>
<ComboBox Grid.Row="12" Grid.Column="1" Name="cbxCombobox1"
        SelectionChanged="cbxCostCentre_SelectionChanged" />
<Label Grid.Row="13" Grid.Column="0" Name="lblCombobox2">
    Select value from Combobox2</Label>
<ComboBox Grid.Row="13" Grid.Column="1" Name="cbxCombobox2"/>

我在主窗口上使用的这个自定义控件如下

<StackPanel Background="LightCyan">
<views:NewAccount HorizontalAlignment="Center" 
    Margin="30" FontSize="14"/>

我需要填充组合框级联以将前一个组合框选择值作为下一个组合框的过滤器参数。 似乎下面的代码可以做到这一点。它正在为 Combobox2 提供值列表进行过滤。但是,我可能遗漏了一些东西,因为带有“where”子句的 LINQ 查询提供的结果与使用 T-SQL 运行时不同。它非常相似,但 Combobox2 中的值或多或少与 T-SQL 列表不同。

using System.Linq;
namespace AccountsSetup.UserControls
{
    public partial class NewAccount : UserControl 
    {
        public NewAccount() 
        {
            InitializeComponent();

            using (SQL.DBDataContext db = new SQL.DBDataContext())
            {
                var allCombobox1s = from t in db.Table1
                                     select t.Name;
                cbxCombobox1.ItemsSource = allCombobox1s;
            }
        }

        private void cbxCombobox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {            
            using (SQL.DBDataContext dbs = new SQL.DBDataContext())
            {
                string value = "";
                if (cbxCombobox1.SelectedIndex >= 0)
                    value = cbxCombobox1.SelectionBoxItem.ToString();
                var allCombobox2s = (from t in dbs.View1
                                       where t.Combobox1.Contains(value)
                                       select t.Name).Distinct(); 
                cbxCombobox2.ItemsSource = allCombobox2s;

            }

我确实尝试将 Combobox2 更改为以下代码。但是,结果是一样的。

<ComboBox Grid.Row="13" Grid.Column="1" x:Name="cbxCombobox2"
    ItemsSource="{Binding}" SelectedValue="{Binding ElementName=cbxCombobox1,
    Path=SelectedItem.Name, Mode=OneWay}" />

请告知可以在代码中进行哪些更正。

谢谢

【问题讨论】:

  • TSQL 和View1 表是什么样的?到底有什么区别?
  • cbxCombobox1.ItemsSource = allCombobox1s 应为 cbxCombobox1.ItemsSource = allCombobox1s.ToList()。同样cbxCombobox2.ItemsSource = allCombobox2s; 应该是cbxCombobox2.ItemsSource = allCombobox2s.ToList();
  • 感谢您添加 .ToList() 的更正建议。我做到了。虽然 View1 中存在所有字段,但我使用 Table1 的原因是 View1 中的一个字段是 Table1 中的主要字段。不过,我也可以从 View1 中获得 Distinct。
  • 我想知道它是否不是多余的,因为 LINQ 查询也以 .ToList() 结束。

标签: c# wpf linq


【解决方案1】:

您应该将第一个 ComboBox 的 SelectedItem 属性的值与视图中 Name 列的值进行比较。

试试这个:

private void cbxCombobox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    using (SQL.DBDataContext dbs = new SQL.DBDataContext())
    {
        string value = cbxCombobox1.SelectedItem as string;
        if (!string.IsNullOrEmpty(value))
        {
            var allCombobox2s = (from t in dbs.View1
                                 where t.Name != null && t.Name.Contains(value)
                                 select t.Name).Distinct().ToList();
            cbxCombobox2.ItemsSource = allCombobox2s;
        }
    }
}

【讨论】:

    猜你喜欢
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    • 2021-12-05
    • 2013-06-15
    • 1970-01-01
    相关资源
    最近更新 更多