Search code examples
c#wpfxamlitemscontrol

Binding SelectedItem in comobox with current ItemSource?


So what I am trying to achieve is to bind the current item from the ItemsSource to the SelectedItem in a Comobox I hope the following (A simplified example) code would demonstrate what I want.

public class book : INotifyPropertyChanged{
private string _title;
private string _author;

public book(){
    this.Title = "";
    this.
}
public string Title{
    get{return _title;}
    set{
        _title = value;
                NotifyPropertyChanged("Title");

    }
}
public string Author{
    get{return _author;}
    set{
        _author = value;
        NotifyPropertyChanged("Author");
    }
}

    public event PropertyChangedEventHandler PropertyChanged;

    protected void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }
}

The ThisViewModel.cs Code:

public class ThisViewModel{
    private ObservableCollection<Book> _book;
    private List<Book> _existedBooks;
    public ObservableCollection<Book> Book{
        get{return _book;}
        set{_book = value;}
    }

    public ThisViewModel(){
        Books = new ObservableCollection<Book>();
        Books.Add(new Book())
    }
    public List<Book> ExistedBooks{
        get{return _existedBooks;}
        set{_existedBooks = value;}
    }
}

The code-behind of the ThisView.xaml.cs:

public partial class ThisView{
    public ThisView(){
        InitializeComponent();
        this.DataContext = new ThisViewModel();

    }
}

The XAML code ThisView.xaml:

<ItemsControl ItemsSource="{Binding Path=Book}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <ComboBox  ItemsSource="{Binding Path=ExistedBooks}"  
            DisplayMemberPath="Title"
            SelectedItem="{Binding <HERE IS MY PROBLEM>}"
            ></ComboBox>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

How can I bind the selectedItem (one of the existedBooks) to the ItemsControl current Item (Book).

Hope I made my point clear enough. Thanks for your time.


Solution

  • You should add the same Book object to both collections for this to work:

    public ThisViewModel()
    {
        Book = new ObservableCollection<Book>();
        ExistedBooks = new List<Book>();
    
        Book bookA = new Book() { Title = "Title A" };
        Book.Add(bookA);
        ExistedBooks.Add(bookA);
    }
    

    Then you can bind the SelectedItem property of the ComboBox to the current item in the ItemsControl like this:

    <ItemsControl ItemsSource="{Binding Path=Book}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <ComboBox  ItemsSource="{Binding DataContext.ExistedBooks, RelativeSource={RelativeSource AncestorType=ItemsControl}}"
                                   SelectedItem="{Binding Path=., Mode=OneWay}"
                                   DisplayMemberPath="Title" />
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>