Mike Malter
Mike Malter

Reputation: 1038

WPF Listbox binding

I have a physician object, and one of its properties is an ObservableList of clinics. It is being used in a window to show the details of a physician. I can get individual properties to bind to TextBox and ComboBox controls, but I can't get the list of clinics to bind to my ListBox.

Here is the xaml for my ListBox:

<ListBox Height="318" 
 HorizontalAlignment="Left" 
 Margin="422,0,0,0" 
 Name="lbClinic" 
 VerticalAlignment="Top" 
 Width="158" 
 SelectedValue="{Binding ClinicID, Path=Clinics, Mode=TwoWay, 
                          UpdateSourceTrigger = PropertyChanged}"
 SelectedValuePath="ClinicID" 
 DisplayMemberPath="Name"
 ItemsSource="{Binding DataContext.ClinicList, 
                          ElementName = PhysicianInfoLookup, Mode = OneWay}" 
 SelectionMode="Multiple" />

The Listbox populates properly with items from the ClinicList which is a list of all possible clinics. However, I cannot get the Clinics list from the physician object to bind so that it's items are selected in the Listbox. I also want to go the other way and if an item is deselected, the ObservableList in the physician object will change accordingly.

How do I two-way bind the ObservableList of Clinics in my physician object to the list of Clinics (ObservableList of clinic objects) in my Listbox?

Thank you.

Upvotes: 8

Views: 70549

Answers (3)

paparazzo
paparazzo

Reputation: 45106

You are going to need to use a template with TextBox for name and ListBox for Clinics and you just bind the internal ListBox path to Clinics. DisplayMemberPath is a short cut a single TextBox. If you want more then you need individual controls.

Upvotes: 1

Adam Robinson
Adam Robinson

Reputation: 185703

Your issue is SelectedValue. At the ListBox level, binding to multiple selection objects is not supported. The only real way to do this with bindings would be to rework your ViewModel so that the list of clinics returned from the binding represents all clinics, and each object there should have an IsSelected (or something similar) property.

You can then use a style to handle the multi selection by adding this XAML within your ListBox node:

<ListBox.ItemContainerStyle>
   <Style TargetType="{x:Type ListBoxItem}">
      <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
   </Style>
</ListBox.ItemContainerStyle>

Upvotes: 3

Phil
Phil

Reputation: 43021

Mike, there are few problems with your bindings. Here's a complete sample demonstrating one way of doing what (I think) you're after.

View:

<Page.Resources>
    <ViewModel:Physician x:Key="physician"/>
</Page.Resources>
<StackPanel DataContext="{StaticResource physician}" >
    <TextBlock Text="{Binding Name}" Background="Orange"/>
    <TextBlock Text="Works in:"/>
    <ListBox ItemsSource="{Binding Clinics}" 
             SelectedValue="{Binding SelectedClinicId}" 
             SelectedValuePath="Id" DisplayMemberPath="Name" />
</StackPanel>

View model:

public class Physician
{
    private int _selectedClinicId;

    public Physician()
    {
        Name = "Overpaid consultant";
        Clinics = new ObservableCollection<Clinic>
                      {
                          new Clinic {Id = 0, Name = "Out Patients"},
                          new Clinic {Id = 1, Name = "ENT"},
                          new Clinic {Id = 2, Name = "GE"},
                      };
    }

    public string Name { get; set; }
    public IEnumerable<Clinic> Clinics { get; private set; }

    public int SelectedClinicId
    {
        get { return _selectedClinicId; }
        set
        {
            if (value != _selectedClinicId)
            {
                Debug.WriteLine(string.Format("setting clinic to: {0}",value));
                _selectedClinicId = value;
            }
        }
    }
}

public class Clinic
{
    public int Id { get; set; }
    public string Name { get; set; }
}

Note that for read/write properties you would probably want to raise property change notifications.

Upvotes: 13

Related Questions