简体   繁体   English

选择时C#绑定子组合框

[英]C# Binding child combobox on selection

i am new on WPF/EF and tring to bind two comboboxes with each other. 我是WPF / EF的新手,正在努力将两个组合框相互绑定。

My code to start the entitys: 我启动实体的代码:

 private ShipperDBEntities shipper;
    public EnterIndicator()
    {
        InitializeComponent();
        shipper = new ShipperDBEntities();
        ReLoad();
    }

In my Reload Sub, i start to query the entitys with LINQ. 在我的Reload Sub中,我开始使用LINQ查询实体。 First, my IndicatorComboBox, the cartype (Ship, car, unobtrusive chaising car and more... types..). 首先,我的IndicatorComboBox是cartype(船舶,汽车,不引人注意的轻便小车等类型)。 The second ComboBox contain all type of definitions. 第二个ComboBox包含所有类型的定义。 BUT when i selected one of the indicators above, the second TypeComboBox sould select the appropriate type of car. 但是,当我选择以上指标之一时,第二个TypeComboBox会选择合适的汽车类型。 The type of the car is definied in the database. 汽车的类型在数据库中定义。 Here is the code to the query: 这是查询的代码:

  DataContext = null;
        var query = (from Fahrzeuges in shipper.Fahrzeuges
                     join typen in shipper.Fahrzeugtypens on Fahrzeuges.Fahrzeugtyp equals
                     typen.FahrzeugTyp_ID
                     where
                       Fahrzeuges.Versandunternehman.Versandunternehmen == LieferantenName
                     select new
                     {
                         Fahrzeuges.Kennzeichen,
                         Fahrzeuges.Fahrzeug_ID,
                         typen.FahrzeugTyp
                     });
        DataContext = query.ToList();


        FahrzeugTypBox.ItemsSource = (from Fahrzeugtypens in shipper.Fahrzeugtypens
                                      select new
                                      {
                                          FahrzeugTyp_ID = Fahrzeugtypens.FahrzeugTyp_ID,
                                          FahrzeugTyp = Fahrzeugtypens.FahrzeugTyp
                                      }).ToList();


        KennzeichenBox.SelectedIndex = 0;

I guess i can solve it, by using the correct xaml. 我想我可以通过使用正确的xaml解决它。 Here is the code for xaml (but i realy dont know how). 这是xaml的代码(但我真的不知道如何)。

     <ComboBox Grid.Row="1" Grid.Column="1" ItemsSource="{Binding}"  SelectedValuePath="Fahrzeug_ID" DisplayMemberPath="Kennzeichen" VerticalAlignment="Top" Name="KennzeichenBox" Height="25" SelectionChanged="KennzeichenBox_SelectionChanged"/>


    <ComboBox Grid.Row="3" IsSynchronizedWithCurrentItem="True" 
              Grid.Column="1" ItemsSource="{Binding}"  
              SelectedValuePath="FahrzeugTyp_id" 
              DisplayMemberPath="FahrzeugTyp"
              SelectedValue="{Binding SelectedItem.FahrzeugTyp,ElementName=KennzeichenBox}"
              VerticalAlignment="Top" 

              Name="FahrzeugTypBox" Height="25"/>

I started to try a solution by using SelectedValue={Binding} from the selection by the first combobox. 我开始通过使用第一个组合框的选择中的SelectedValue = {Binding}来尝试解决方案。

Any suggestions? 有什么建议么? Thanks! 谢谢!

Try this: 尝试这个:

       DataContext = this;

       public List<dynamic> Fahrzeuges { get; set; }
       public List<dynamic> Typen { get; set; }


       Fahrzeuges =  new List<dynamic>(from Fahrzeuges in shipper.Fahrzeuges
                     join typen in shipper.Fahrzeugtypens on 
                     Fahrzeuges.Fahrzeugtyp equals
                     typen.FahrzeugTyp_ID
                     where Fahrzeuges.Versandunternehman.Versandunternehmen == LieferantenName
                     select new
                     {
                         Kennzeichen = Fahrzeuges.Kennzeichen,
                         Fahrzeug_ID = Fahrzeuges.Fahrzeug_ID,
                         FahrzeugTyp = typen.FahrzeugTyp
                     });

      Typen = new List<dynamic>(from Fahrzeugtypens in shipper.Fahrzeugtypens
                                  select new
                                  {
                                      FahrzeugTyp_ID = Fahrzeugtypens.FahrzeugTyp_ID,
                                      FahrzeugTyp = Fahrzeugtypens.FahrzeugTyp
                                  });

then in xaml: 然后在xaml中:

<ComboBox Grid.Row="1" Grid.Column="1" ItemsSource="{Binding Fahrzeuges}"
          SelectedValuePath="Fahrzeug_ID" DisplayMemberPath="Kennzeichen"
          VerticalAlignment="Top" Name="KennzeichenBox" Height="25"
          SelectionChanged="KennzeichenBox_SelectionChanged"/>


<ComboBox Grid.Row="3" Grid.Column="1" ItemsSource="{Binding Typen}"  
          SelectedValuePath="FahrzeugTyp" 
          DisplayMemberPath="FahrzeugTyp"
          SelectedValue="{Binding ElementName=KennzeichenBox, Path=SelectedItem.FahrzeugTyp}"
          VerticalAlignment="Top" 
          Name="FahrzeugTypBox" Height="25"/>

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

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