Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 带LINQ的WPF级联组合框_C#_Wpf_Linq - Fatal编程技术网

C# 带LINQ的WPF级联组合框

C# 带LINQ的WPF级联组合框,c#,wpf,linq,C#,Wpf,Linq,我在自定义控件上有许多组合框,如下所示 <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="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"/>
我尝试将Combobox2更改为以下代码。但是,结果是一样的

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

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


谢谢

您应该将第一个组合框的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;
        }
    }
}

您应该将第一个组合框的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;
        }
    }
}

TSQL和
View1
表是什么样子的?具体区别是什么?
cbxcombox1.ItemsSource=allcombox1s
应为
cbxcombox1.ItemsSource=allcombox1s.ToList()
。类似地
cbxcombox2.ItemsSource=allcombox2s
应该是
cbxcombox2.ItemsSource=allcombox2s.ToList()感谢您对add.ToList()的更正建议。我做到了。虽然所有字段都存在于View1中,但我使用Table1的原因是View1中的一个字段也是Table1中的一个主要字段。但是,我也可以使用Distinct从View1获得它。我想知道它是否是多余的,因为LINQ查询也是以.ToList()结束的。TSQL和
View1
表是什么样子的?具体区别是什么?
cbxcombox1.ItemsSource=allcombox1s
应为
cbxcombox1.ItemsSource=allcombox1s.ToList()
。类似地
cbxcombox2.ItemsSource=allcombox2s
应该是
cbxcombox2.ItemsSource=allcombox2s.ToList()感谢您对add.ToList()的更正建议。我做到了。虽然所有字段都存在于View1中,但我使用Table1的原因是View1中的一个字段也是Table1中的一个主要字段。但是,我也可以使用Distinct从View1获得它。我想知道它是否是多余的,因为LINQ查询也以.ToList()结束。
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;
        }
    }
}