简体   繁体   中英

C# Binding child combobox on selection

i am new on WPF/EF and tring to bind two comboboxes with each other.

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. First, my IndicatorComboBox, the cartype (Ship, car, unobtrusive chaising car and more... types..). The second ComboBox contain all type of definitions. BUT when i selected one of the indicators above, the second TypeComboBox sould select the appropriate type of car. 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. Here is the code for xaml (but i realy dont know how).

     <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.

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:

<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"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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