Click here to Skip to main content
14,732,943 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all i want to bind my listbox depends on combobox selected item here is my code

<StackPanel Orientation="Horizontal" >
            <ComboBox Name="cmbID" Width="150"  Margin="10" Height="30" SelectedItem="{Binding CmbSelected,Mode=TwoWay}" DisplayMemberPath="ID" ItemsSource="{Binding MyStudent,Mode=TwoWay}"/>
            <Button Name="btnGetDetail"  Margin="10" Command="{Binding getDetails}" Content="Get Details" Height="30" Width="90"/>
            <TextBox Name="tbName1" Width="90" Height="30" Text="{Binding ElementName=cmbID,Path= SelectedItem.Sub}"></TextBox>
        </StackPanel>

In Above code i am binding my combobox to one observable collection and want to bind my ListView to selected item of combobox below is my code


<ListView.View >
              <GridView x:Name="grdStudentDetails">
                  <GridViewColumn Header="ID" DisplayMemberBinding="{Binding ElementName=cmbID,Path=SelectedItem.ID}" Width="30"/>
                  <GridViewColumn Header="Name" DisplayMemberBinding="{Binding ElementName=cmbID,Path =SelectedItem.Name}" Width="100"/>
                  <GridViewColumn Header="RollNum" DisplayMemberBinding="{Binding ElementName=cmbID,Path=SelectedItem.RollNum}" Width="100"/>
                  <GridViewColumn Header="Subject" DisplayMemberBinding="{Binding ElementName=cmbID,Path=SelectedItem.Sub}" Width="100"/>
                  <GridViewColumn Header="PhNumber" DisplayMemberBinding="{Binding ElementName=cmbID,Path=SelectedItem.PhNum}" Width="100"/>
              </GridView>
          </ListView.View>
      </ListView>


I am not able to find where I am doing mistake even I bind my text box with same binding it is working fine. please refer textbox just below my combobox's XAML.


What I have tried:

I am not able to find where I am doing mistake even I bind my text box with same binding it is working fine. please refer textbox just below my combobox's XAML.
Posted
Updated 29-Mar-19 12:18pm
Comments
sumitk.cadc 2-Dec-16 2:32am
   
private student cmbSelcted;

public student CmbSelected
{
get { return cmbSelcted; }
set { cmbSelcted = value; OnPropertyChanged("CmbSelected"); }
}


public ObservableCollection<student> MyStudent
{
get { return myStudent; }
set { myStudent = value; OnPropertyChanged("MyStudent"); }
}

You aren't implementing the ListView data binding correctly. ListView is designed to bind to an object that implements IEnumerable. The items in your combo box must be something that is enumerable like collections, arrays, etc. The ListView needs to bind to the SelectedItem property of the combobox. So something like:
<ListView ItemsSource="{Binding ElementName=cmbID, Path=SelectedItem}">


When a user makes a choice in the combobox the SelectedItem property changes which the ListView will detect via the binding. The ListView will then set it's DataContext to the selected item. This is why the items in your combobox must all implement IEnumerable. When the ListView sets it's DataContext to the selected item, it will iterate over that item to create an entry for each entry in the selected item. The key here is that your bindings for the columns are relative to a single item. So they should just be {Binding ID} and {Binding RollNum} etc.
   
Comments
sumitk.cadc 2-Dec-16 2:30am
   
<listview name="myStudent" itemssource="{Binding ElementName=cmbID,Path=SelectedItem,UpdateSourceTrigger=PropertyChanged}" horizontalalignment="Left" width="420" height="150">
<listview.view>
<!--<gridview x:name="grdStudentDetails">
<gridviewcolumn header="ID" displaymemberbinding="{Binding ElementName=cmbID,Path=SelectedItem.ID}" width="30">
<gridviewcolumn header="Name" displaymemberbinding="{Binding ElementName=cmbID,Path =SelectedItem.Name}" width="100">
<gridviewcolumn header="RollNum" displaymemberbinding="{Binding ElementName=cmbID,Path=SelectedItem.RollNum}" width="100">
<gridviewcolumn header="Subject" displaymemberbinding="{Binding ElementName=cmbID,Path=SelectedItem.Sub}" width="100">
<gridviewcolumn header="PhNumber" displaymemberbinding="{Binding ElementName=cmbID,Path=SelectedItem.PhNum}" width="100">
-->
<gridview x:name="grdStudentDetails">
<gridviewcolumn header="ID" displaymemberbinding="{Binding Id}" width="30">
<gridviewcolumn header="Name" displaymemberbinding="{Binding Name}" width="100">
<gridviewcolumn header="RollNum" displaymemberbinding="{Binding RollNum}" width="100">
<gridviewcolumn header="Subject" displaymemberbinding="{Binding Sub}" width="100">
<gridviewcolumn header="PhNumber" displaymemberbinding="{Binding PhNum}" width="100">





i did accordingly but still there is no solutions,i am also sharing my viewmodel.cs class plz have look on it
Jason Gleim 2-Dec-16 9:16am
   
Instead of binding the listview to the combobox, just bind it directly to the selected student:
<listview name="myStudent" itemssource="{Binding CmbSelected, Mode=OneWay}" horizontalalignment="Left" width="420" height="150">

There is no need to bind to the UI element when the ViewModel exposes the property you want. That is how you should be binding data anyway... always to the ViewModel.

You don't show what your student class looks like but I'm guessing it isn't implementing IEnumerable. Check your debugging output and you *may* find a type casting error indicating the ListView can't cast student to IEnumerable.

I have a feeling you are just using the wrong control. When you choose a student in the combobox, you are just choosing one student, right? That isn't what a ListView is for. If this is true, you should just use a stack panel and textblocks.
sumitk.cadc 2-Dec-16 22:24pm
   
i want to show one student detail in the listview which id was selected by combobox item.
drive.google.com/open?id=0B4fArMytRDVPREF3bDZBT0tudEk
i have upload my code plz have a look on it.plz look on right side combobox and below listview.
and is it possible to bind command to listview or combobox selected item?
i want if i select any row in listview then it should automatically reflect in text box.
i did it using binding command on button for update.after clicking it is reflecting in my textbox.
Thanks
I found the answer.  It was as simple as I first thought.  The ComboBox was to be populated with victim data when the user selected a victim.  

I figured it had to be a simple binding to the selected item, and it is.  Here is how it is done for the InvolvementNumber, which is victim property. VictimNames is the ComboBox, and the Path got me there. 

 <StackPanel Grid.Row="0" Grid.Column="0">
     <TextBlock> #</TextBlock>
     <TextBlock Name="VictimNumber"  
         Style="{StaticResource PartIIGridIsEnabledTextBlock}" 
         Text="{Binding ElementName=VictimNames,Path=SelectedItem.InvolvementNumber}"/> </StackPanel>
   
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900