ListBox Binding in WPF

Select the color in list box,

Select blue

Image-1.jpg
 
Select RED

Image-2.jpg

XAML CODE

<Window x:Class="anim.MainWindow"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

Title="Anim in wpf" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

Height="300" Width="400">

      <Canvas>

            <StackPanel Width="200">                 

                  <TextBlock  Height="24" Text="Colors :" FontSize="15"/>

                  <ListBox x:Name="lbColor"  Height="56">

                        <ListBoxItem Content="Blue" />

                        <ListBoxItem Content="Red"/>

                        <ListBoxItem Content="Beige"/>

                        <ListBoxItem Content="CadetBlue"/>

                        <ListBoxItem Content="Gold"/>

                        <ListBoxItem Content="Green"/>

                        <ListBoxItem Content="LightGreen"/>

                        <ListBoxItem Content="Yellow"/>

                        <ListBoxItem Content="Plum"/>

                        <ListBoxItem Content="Black"/>

                        <ListBoxItem Content="Brown"/>

                        <ListBoxItem Content="Purple"/>

                        <ListBoxItem Content="Orange"/>

                        <ListBoxItem Content="Salmon"/>

                        <ListBoxItem Content="BurlyWood"/>

                        <ListBoxItem Content="Aqua"/>

                        <ListBoxItem Content="Gray"/>

                        <ListBoxItem Content="HoneyDew"/>

                        <ListBoxItem Content="Tan"/>

                        <ListBoxItem Content="Teal"/>

                        <ListBoxItem Content="Tomato"/>                  

                  </ListBox>

                  <TextBlock  Height="25" Text="You Selected :" />

                  <TextBlock  Height="150">

        <TextBlock.Text>

          <Binding ElementName="lbColor" Path="SelectedItem.Content"/>

        </TextBlock.Text>

                    <TextBlock.Background>

                   <SolidColorBrush  Color="{Binding SelectedItem.Content,ElementName=lbColor}"/>                     

        </TextBlock.Background>                   

      </TextBlock>             

    </StackPanel>       

  </Canvas>

</Window>

Ebook Download
View all
Learn
View all