.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
david stephan
Gaurav Pal
Post New Web Links

How to Bind ListBox to ComboBox's SelectedItem

Posted By:      Posted Date: September 13, 2010    Points: 0   Category :WPF
I have a ComboBox bound to a list of custom objects (Person). Each Person has a Friends collection. I want a ListBox below the ComboBox to contain a list of Friends depending on the Person selected in the ComboBox above it.   Something like: <ComboBox x:Name="MyComboBox" ItemsSource="{Binding People}"/> <ListBox ItemsSource="{Binding Element=MyComboBox, Path=SelectedItem.Friends}"/>   It doesn't work (null reference, I'm assuming because the SelectedItem.Friends is the wrong syntax.   Any pointers?

View Complete Post

More Related Resource Links

Binding ListBox to ComboBox's SelectedItem

Hi All,   I have a ComboBox that is bound to a collection of People. When the user selects a Person in the ComboBox, I want a ListBox below that to be populated wtih the Person's Friends.   Is it something like:   <ComboBox x:Name="PeopleComboBox" ItemsSource="{Binding People}"/> <ListBox ItemsSource="{Binding ElementName=PeopleComboBox, Path=SelectedItem.Friends}"/>   I'm going for something like that.

Bind An Already Bound TextBox to ComboBox SelectedItem


Hey all,


If I have a TextBox that is already two-way bound to something, but I also want it so when a ComboBox SelectedItem changes, then the TextBox's Text property is set to the new SelectedItem.


Here's what I have so far, I added the comment where the TextBox is, which I would like to also be bound to the ComboBox below this.


        <ListView Grid.Row="2" 
             DataContext="{Binding ScriptParameters}"

ActiveX and Visual Basic: Enhance the Display of Long Text Strings in a Combobox or Listbox


The combobox and listbox controls in Visual Basic have no built-in support for displaying text strings that are too long to fit in the visible area of either control. This article describes several techniques to improve the readability of long strings in each control. The first technique uses tooltips to display a combobox or listbox item. The other techniques, which use various Windows APIs, include adding a horizontal scrollbar for a listbox and determining the necessary width of the longest string given the font attributes of the text. The required steps are described for both comboboxes and listboxes.

John Calvert

MSDN Magazine December 2000

How to bind to DisplayMemberPath in a customercontrol that derived from ComboBox?

Hello, All: I need a customer control which derives from ComboBox. In its resource file I have the style:   <Style TargetType="{x:Type local:MyComboBox}">             <Setter Property="SnapsToDevicePixels" Value="True"></Setter>             <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>             <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>             <Setter Property="ScrollViewer.CanContentScroll" Value="True"/>         <Setter Property="ItemTemplate">                 <Setter.Value>                     <HierarchicalDataTemplate>                     <CheckBox Content="{Binding Path=DisplayMemberPath,Mode=TwoWay,RelativeSource={RelativeSource

Sorting a ListBox or ComboBox on a Converted Value

Is it possible to apply a Converter to the SortDescription of a CollectionViewSource?  My ViewModel contains a List<> of a class that contains an enum and a Boolean.  My XAML displays a multi-select ListBox, showing the localized names of the enum values using a Converter in the View, and binds each ListBoxItem's IsSelected to the Boolean in the members of the bound List<>.  That's working.  Now I want to sort the ListBox items by those localized enum names, without reordering the List<> in the ViewModel.  Is this possible, and if so, how can it be done?

page.setfocus(listbox.selecteditem) is not setting the focus if index > 820

Hi All, Does any one know the reason Why I am unable to setfocus to my list box if the index > 820 My list box contains 2000 items and the index is not  able to set the focus if the index > 820  

ListBox.SelectedItem stuck at whatever is first selected and other strange ListBox behavior

Hi, I've built a List<Participant> (Participant being my custom type) and assigned this to ListBox.ItemsSource. (I'm aware this is more the Windows Forms way than the WPF way of databinding, but I'd still like to understand what's going on here - even though suggestions on how to perform my task in a more WPF-y way are also welcome.) I've set ListBox.SelectionMode to Single, and attached an event handler to the SelectionChanged event. In this handler, I dump the SelectedIndex and SelectedItem to debug output. Contrary to my expectation, 1) SelectedIndex is always -1. This isn't really causing me any trouble, but it's certainly not what I'd expect. 2) SelectedItem refers to the correct object the first time (in the windows lifetime) I select an item, but then keeps referring to this first-selected item regardless of whether I deselect (by clicking the item again while holding down the CTRL key) or select some other item. 3) If I keep clicking around and scrolling a bit (the list has ~320 items) within the list, now and then the list suddenly displays a bunch of items - sometimes many in a row, sometimes non-contiguous items, but it seems always "nearby" items! - as if they were selected. The list keeps firing the SelectionChanged event, but SelectedIndex is forever -1 and SelectedItem (and SelectedValue) always refers to whatever I had selected the fir

WPF Combobox Bind FallbackValue to Property

What I am wanting to do seems fairly simple, but WPF doesn't seem to like it. Basically I am binding a ComboBox's SelectedValue to my object:       <ComboBox SelectedValue="{Binding SendToBinLocation}" Name="cboToBin" Width="100" DisplayMemberPath="BinLocation" SelectedValuePath="BinLocation" ItemsSource="{Binding ToBinList, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" BorderBrush="{Binding Path=SendToBinLocation, Converter={StaticResource MissingBinConverter}, Mode=OneWay}"> <ComboBox.ItemTemplate> <DataTemplate> <TextBlock FontSize="16" Text="{Binding BinLocation}"/> </DataTemplate> </ComboBox.ItemTemplate> </ComboBox>  This works fine. However, I am wanting to specify a default value for the combobox in the event ToBinLocation is empty or null. I can achieve this by hard-coding in FallBackValue=somevalue, but this is not what I want to do. What I would like to do is bind the FallBackValue to another property of the object. You can't databind to fallBackValue because it is a dependency property, so how would I go about specifying the FallBackValue with another property on the object? The idea here is that if the SendToBinLocation is null or empty, the Comboxvalu

Combobox DisplayMembertPath Setted : Never change SelectedItem



I begin with WPF and binding. In my project, i have one Combobox which is binded (ItemsSource) to an ObservableCollection<Organization> (dependency property in MainWindow code-behind).

My class Organization contains a property called "Name" and i want to display it in items, so i set DisplayMembertPath to "Name".

But when i do that, if i change the item in the combobox, the item doesn't change instead of code-behind side, the SelectedItem changes.

I tried to use a DataTemplate with a TextBlock binded to the Name property, i have the same issue.


Here my code-behind (MainWindow.xaml.cs) :


public ObservableCollection<Organization> OrganizationCollection
   get { return (ObservableCollection<Organization>)GetValue(OrganizationCollectionProperty); }
   set { SetValue(OrganizationCollectionProperty, value); }
  public static readonly DependencyProperty OrganizationCollectionProperty = 

ComboBox with bound SelectedItem displays new item even if selection fails internally



I'm a WPF beginner and currently exploring some techniques and use cases. This is one of my test setups:


<Window x:Class="wpfTest.MainWindow"
    Title="MainWindow" Height="350" Width

Bind listbox to gridview on client side



I have a list box (that uses a SQlDataSource) that has a list of jobs. I have a grid view (that also uses a SQLDataSource) that has the items used in each job. I want to bind the list box value to the gridview on the client side, i.e. when the user clicks on a different job in the list view I want the grid to show the items for that job.

Also, at formload I want the gridview to show the details of the first job on the listview.

Please let me know how I can do this in the aspx page.


- Sankaran

How to Data bind to a listbox on a wpf usercontrol ?


Hi ,

what i have is a Windows form and in that form i have a elementhost which hosts a wpf usercontrol. On that control is a wpf listbox. In the windows form i have a routine that fetches data form the db.

How can i bind that data to the list box using the "{Binding = ???} attribute in the XAML ?

Currently i bubble up an event and on the event in the windows form i call an action whcih binds to the listbox. is that the correct way to do it



Bind combox, ListBox to SQL database column


Hi everyone

  I am having problem where I want to bind a column of the database to the Combobox so that Item of the combobox are present in the database

for example I have a table named Animals which is in the following format

ID   Animal

1    Dog

2    Cat

3    Horse

4   Goat


So the Item of the Combobox are Dog, Cat, Horse and Goat.  I had tried to bind the database to ObservableCollection First and then bind that ObservableCollection to the combobox.

   public class NameList : List<PersonName>
	private LikesLINQDataContext newlikes = new LikesLINQDataContext();

	List<string> depart = new List<string>();

	 public NameList(): base()
		foreach (var lol in newlikes.Likes)

		foreach (stri

Unable to bind a List of a [DataContract] class to a ListBox




I have a class who is used by Silverlight though RIA Services and who contains the [DataContract] and on its properties [DataMember].

That class is also used by my WPF Application and i have a List<myClass> which i want to bind it on ListBox.


The problem is when i bind the list, when i select an item in my listbox, the first item is always selected and the right-click doesn't work properly.

In fact, the behavior of UI is weird and badly refreshed.

But, before, the class didn't have DataContract/DataMember attributes and it worked fine.


Do you know, a workaround to continue to use that class and avoid to create an other class specially for databind ?





How do I bind the ModelItem collection to combobox in XAML



I need to create an custom activity with a combo box that need to display all the activity name within the same parent.

Below are the code snippets that failed to list out the activities name. Anyone has any ideas to make this work?

<ComboBox Name="ActivityList" 
  ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type sas:Sequence}},Path=ModelItem.Activities}"


Bind custom list to combobox


Hi, I have a custom list which I newed to binf to a combo box, my class is as followes

Class ComboData
    Public id As String
    Public V1 As String
    Public V2 As String
End Class

I am trying the following but nothing is dispalyed in the combobox, no errors....

        Dim Listdata As New List(Of ComboData)()
        Listdata.Add(New ComboData() With {.id = "1", .V1 = "test", .V2 = "test2"})
        Listdata.Add(New ComboData() With {.id = "2", .V1 = "test2", .V2 = "test3"})
        cboSmtpServer.ItemsSource = Listdata
        cboSmtpServer.DisplayMemberPath = "V1"
        cboSmtpServer.SelectedValuePath = "id"
        cboSmtpServer.SelectedIndex = 0
        cboSmtpServer.SelectedValue = "V1"

What is missing


Thanks in advance

How to set a wpf combobox displaymemberitem to a column in 1 table and bind text displayed to a colu


I have a combobox that has a datacontext set to a 'Customer Type' that is a customer CRUD window in WPF that itself has a datacontext of 'Customer' - when i scroll through my customer table entries that have been populated into a listbox, all of the corresponding textboxes on my CRUD window update to reflect the values stored in the Customer table except the combobox.  If I change the datacontext of the combobox to be 'Customer' the text updates correctly when scrolling through but the dropdown contents are now duplicated to match the number of records.  So my problem is this;

How can I display the itemssource as the 'Customer Type.Customer_Type' datacontext, but display the Text as 'Customer.Customer_Type'?

The closest i could compare it to would be a winforms example of setting the combo's DataSource to 'Customer_Type' on the combo with a displaymember of 'Customer_Type' and then making the DataBindings 'Text' property of the combo to be 'Customer.Customer_Type' so it displays the value stored in customer but as soon as you drop it down it shows non-repeated values that are contained in customer_type.customer_type.

Right now in wpf if the datacontext is customer, the combo displays customer_type as follows:






ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend