Click here to Skip to main content
15,896,392 members
Articles / Desktop Programming / WPF

SelectedValuesListBox - A ListBox that implements more than one SelectedValue (SelectedValues)

Rate me:
Please Sign up or sign in to vote.
4.10/5 (5 votes)
20 Aug 2009CPOL3 min read 27.1K   382   11  
Using this, you could set the selection of a ListBox containing languages by binding it to a list of Guids.
<Window x:Class="Sandrino.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:Sandrino"
    Title="Window1" Height="419" Width="868">
    <Grid>
        <local:SelectedValuesListBox ItemsSource="{Binding Path=Languages}"
            SelectedValues="{Binding Path=Me.LanguageIDs}"
            SelectedValuePath="ID" Margin="12,53,322,12" x:Name="listLanguages" SelectionMode="Multiple">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <Label Content="{Binding Name}" FontWeight="Bold"/>
                            <Label Content="{Binding ID}" FontStyle="Italic"/>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
        </local:SelectedValuesListBox>
        
        <TextBlock Height="21" HorizontalAlignment="Left" Margin="12,26,0,0" Name="textBlock1" VerticalAlignment="Top" Width="161">List</TextBlock>
        <ListBox ItemsSource="{Binding ElementName=listLanguages, Path=SelectedValues}" Margin="0,53,12,12" Name="listSelection" HorizontalAlignment="Right" Width="304" />
        <TextBlock Height="21" Margin="0,26,155,0" Name="textBlock2" VerticalAlignment="Top" Text="Selection" HorizontalAlignment="Right" Width="161"></TextBlock>
        <Button Height="23" HorizontalAlignment="Right" Margin="0,12,12,0" Name="button1" VerticalAlignment="Top" Width="114" Click="button1_Click">Show my languages</Button>
    </Grid>
</Window>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Technical Lead RealDolmen
Belgium Belgium
I'm a Technical Consultant at RealDolmen, one of the largest players on the Belgian IT market: http://www.realdolmen.com

All posts also appear on my blogs: http://blog.sandrinodimattia.net and http://blog.fabriccontroller.net

Comments and Discussions