Results 1 to 3 of 3

Thread: How to bind a listbox and combobox?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602

    How to bind a listbox and combobox?

    Hi!

    I want to show the exact same content in a listbox and a combobox. Is there an easy way to bind the items collection of the listbox to the items collection to the combobox?

    Here is my code:
    Code:
    <Window x:Class="BetterTabView.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:b="BetterWpfControls"
            Title="MainWindow" Height="350" Width="525">
        <StackPanel Orientation="Horizontal">
            <ListBox x:Name="myListBox" Width="180">
                <ListBoxItem>Donald</ListBoxItem>
                <ListBoxItem>Mickey</ListBoxItem>
                <ListBoxItem>Daisy</ListBoxItem>
                <ListBoxItem>Goofy</ListBoxItem>
            </ListBox>
    
            <ComboBox  Width="180" Height ="20" x:Name="cmbItems" ItemsSource="{Binding myListBox.Items, Mode=OneWay}" >
            </ComboBox>
        </StackPanel>
    
           
       
    </Window>
    kind regards
    Henrik

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: How to bind a listbox and combobox?

    Not the way to go. Create the list external to both controls and then bind both that one list.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602

    Re: How to bind a listbox and combobox?

    Hi!

    Thanks for the info, however I realise now that my example was a bit too simplified, and the bigger picture is a bit more complex, so maybe you can help me with that.

    The reason I asked this is that we have a tabcontrol with a list of tabs, and my task is to implement a combobox as a tab with a "shortcut" to each of the tabs.

    The tab in a usercontrol has the following data context:
    Code:
    DataContext="{Binding TabsViewModel, Source={StaticResource UnityViewModelResolver}}"
    I managed to find someone that could explain to me that they use Prism and unity (I have never worked with these) and that the tab is a "region" and each of the tabs are "views", and that somewhere in the "unity black box" is a Views collection in the view model which I can access like this:

    Code:
    IEnumerable views = container.Resolve<IRegionManager>().Regions["tabregion"].Views;
    These "views" can be of any kind possible that inherit from a base class.

    So, the reason why I asked this simple and non MVVM question was that I have absolutely no idea how to bind my combobox to this Unity collection of views, which is what the tab control is made up of. In the tabcontrol view they simply access properties like the header image like this (they override the default tab header style:

    Code:
    <Image
                                        Grid.Column="0"
                                        Height="16"
                                        Width="16"
                                        DataContext="{Binding Content.DataContext.HeaderIcon, RelativeSource={RelativeSource AncestorType={x:Type TabItem}}, FallbackValue={x:Null}}" 
                                        Source="{Binding}"
                                        Margin="5,5,5,5"
                                        HorizontalAlignment="Center" 
                                        VerticalAlignment="Center"/>
    So somehow they can now access the object in the views collection directly by using "Binding Content.DataContext.HeaderIcon" and I have no idea how this is possible, what happened to the views collection?

    I am not sure if you get a better idea about my question now? I don't have many hours allocated to this task, and no one here can explain prism/unity so I figured I would take the "quick" way to bind directly to the Items collection of the tab control instead of trying to figure out how to bind to the Unity and it's "Views" collection...

    /Henrik

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width