473,396 Members | 2,076 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

In WPF C#, How to Bind the Image Source according the selected item??

I have a image that is changing according to the selected image also.
Ex.
Code:
Expand|Select|Wrap|Line Numbers
  1. <Image Name="ImagesStatic" />
  2.  
  3. <Button Name="btn1">   <!-- When I choose this the Image -->
  4. <Button.Header>        <!-- Source of img1 copy transfer -->
  5.    <StackPanel>        <!-- ImagesStatic -->
  6.      <Image name="img1">
  7.    <StackPanel>
  8. </Button.Header>
  9. </Button>
  10.  
  11.  
  12. <Button Name="btn1">
  13. <Button.Header>      <!-- ViseVersa -->
  14.    <StackPanel>
  15.      <Image>
  16.    <StackPanel>
  17. </Button.Header>
  18. </Button>
Sep 19 '11 #1

✓ answered by Frinavale

You don't need to use the code behind to accomplish this.
For example, if you had a list of images you could bind the source of the image within the button to the image selected in the list using the ElementName attribute in the Binding statement.

For example:
Expand|Select|Wrap|Line Numbers
  1. <DockPanel Background="CornflowerBlue">
  2.     <DockPanel.Resources>
  3.         <x:Array x:Key="ImageLocations"  Type="sys:String">
  4.             <sys:String>C:\Users\Public\Pictures\Sample Pictures\Chrysanthemum.jpg</sys:String>
  5.             <sys:String>C:\Users\Public\Pictures\Sample Pictures\Desert.jpg</sys:String>
  6.             <sys:String>C:\Users\Public\Pictures\Sample Pictures\Hydrangeas.jpg</sys:String>
  7.             <sys:String>C:\Users\Public\Pictures\Sample Pictures\Jellyfish.jpg</sys:String>
  8.             <sys:String>C:\Users\Public\Pictures\Sample Pictures\Koala.jpg</sys:String>
  9.             <sys:String>C:\Users\Public\Pictures\Sample Pictures\Lighthouse.jpg</sys:String>
  10.             <sys:String>C:\Users\Public\Pictures\Sample Pictures\Penguins.jpg</sys:String>
  11.             <sys:String>C:\Users\Public\Pictures\Sample Pictures\Tulips.jpg</sys:String>
  12.         </x:Array>
  13.     </DockPanel.Resources>
  14.     <ScrollViewer>
  15.         <ListView x:Name="Images" ItemsSource="{StaticResource ImageLocations}" Background="Transparent">
  16.             <ListView.ItemTemplate>
  17.                 <DataTemplate>
  18.                     <Image Source="{Binding }" Height="50"/>
  19.                 </DataTemplate>
  20.             </ListView.ItemTemplate>
  21.         </ListView>
  22.     </ScrollViewer>
  23.     <Button Margin="20" Height="100">
  24.         <Grid>
  25.             <Image Source="{Binding SelectedValue, ElementName=Images}" />
  26.             <TextBlock Text="The Button" FontSize="18" VerticalAlignment="Center" HorizontalAlignment="Center" FontWeight="Bold">
  27.                 <TextBlock.Effect>
  28.                     <DropShadowEffect BlurRadius="18" Color="White" ShadowDepth="0" Opacity="1"/>
  29.                 </TextBlock.Effect>
  30.             </TextBlock>
  31.         </Grid>
  32.     </Button>
  33. </DockPanel>
In your case, you want your list to be 2 buttons...
Create a source that contains 2 pictures

Then, set this list to be the ItemsSource for the ListView displaying the options.

Then, set the ListView.ItemTemplate to display buttons with Images:

Expand|Select|Wrap|Line Numbers
  1. <StackPanel Background="Black">
  2.     <StackPanel.Resources>
  3.         <x:Array x:Key="ImageLocations"  Type="sys:String">
  4.             <sys:String>C:\Users\Public\Pictures\Sample Pictures\Desert.jpg</sys:String>
  5.             <sys:String>C:\Users\Public\Pictures\Sample Pictures\Koala.jpg</sys:String>
  6.         </x:Array>
  7.     </StackPanel.Resources>
  8.     <Image x:Name="theImage" Height="200" Source="{Binding SelectedValue, ElementName=Images}"/>
  9.     <ListView x:Name="Images" ItemsSource="{StaticResource ImageLocations}" 
  10.                 SelectedIndex="0" 
  11.                 Background="Transparent" 
  12.                 Margin="5,10"
  13.                 BorderThickness="0">
  14.         <ListView.Resources>
  15.             <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" />
  16.             <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" />
  17.         </ListView.Resources>
  18.         <ListView.ItemsPanel>
  19.             <ItemsPanelTemplate>
  20.                 <StackPanel Orientation="Horizontal" HorizontalAlignment="Center"></StackPanel>
  21.             </ItemsPanelTemplate>
  22.         </ListView.ItemsPanel>
  23.         <ListView.ItemTemplate>
  24.             <DataTemplate>
  25.                 <Button IsHitTestVisible="False" Height="50"  Width="50">
  26.                     <Image Source="{Binding}" Height="20"/>
  27.                 </Button>
  28.             </DataTemplate>
  29.         </ListView.ItemTemplate>
  30.     </ListView>
  31. </StackPanel>
-Frinny

3 24055
arie
64
If I understood correctly, you want to change the source of an image depending on which button was clicked. Then just use button's Click event and set new source there. You should do something like this:

First add your images to the project and set their building action to Resource.

Then in xaml:
Expand|Select|Wrap|Line Numbers
  1. <Window x:Class="test.MainWindow"
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4.         Title="MainWindow" Height="350" Width="525">
  5.     <Grid>
  6.         <Grid.RowDefinitions>
  7.             <RowDefinition  Height="Auto"/>
  8.             <RowDefinition  Height="*"/>
  9.         </Grid.RowDefinitions>
  10.         <Grid.ColumnDefinitions>
  11.             <ColumnDefinition />
  12.             <ColumnDefinition />
  13.         </Grid.ColumnDefinitions>
  14.         <Image x:Name="MyImageControl" Grid.Row="1"  Grid.Column="0" Source="./Media/img.png" />
  15.         <StackPanel Grid.Row="1"  Grid.Column="1">
  16.             <Button x:Name="b1" Click="b1_Click">Button1</Button>
  17.             <Button x:Name="b2" Click="b2_Click">Button2</Button>
  18.         </StackPanel>
  19.     </Grid>
  20. </Window>
  21.  
and in code behind:
Expand|Select|Wrap|Line Numbers
  1.         private void b1_Click(object sender, RoutedEventArgs e)
  2.         {
  3.             BitmapImage img = new BitmapImage();
  4.             img.BeginInit();
  5.             // You set your Uri here, this example uses resources
  6.             img.UriSource = new Uri("./Media/img.png", UriKind.Relative);
  7.             img.EndInit();
  8.  
  9.             MyImageControl.Source = img;
  10.         }
  11.  
  12.         private void b2_Click(object sender, RoutedEventArgs e)
  13.         {
  14.             BitmapImage img = new BitmapImage();
  15.             img.BeginInit();
  16.             // You set your Uri here, this example uses resources
  17.             img.UriSource = new Uri("./Media/img2.png", UriKind.Relative);
  18.             img.EndInit();
  19.  
  20.             MyImageControl.Source = img;
  21. }
Sep 19 '11 #2
Frinavale
9,735 Expert Mod 8TB
You don't need to use the code behind to accomplish this.
For example, if you had a list of images you could bind the source of the image within the button to the image selected in the list using the ElementName attribute in the Binding statement.

For example:
Expand|Select|Wrap|Line Numbers
  1. <DockPanel Background="CornflowerBlue">
  2.     <DockPanel.Resources>
  3.         <x:Array x:Key="ImageLocations"  Type="sys:String">
  4.             <sys:String>C:\Users\Public\Pictures\Sample Pictures\Chrysanthemum.jpg</sys:String>
  5.             <sys:String>C:\Users\Public\Pictures\Sample Pictures\Desert.jpg</sys:String>
  6.             <sys:String>C:\Users\Public\Pictures\Sample Pictures\Hydrangeas.jpg</sys:String>
  7.             <sys:String>C:\Users\Public\Pictures\Sample Pictures\Jellyfish.jpg</sys:String>
  8.             <sys:String>C:\Users\Public\Pictures\Sample Pictures\Koala.jpg</sys:String>
  9.             <sys:String>C:\Users\Public\Pictures\Sample Pictures\Lighthouse.jpg</sys:String>
  10.             <sys:String>C:\Users\Public\Pictures\Sample Pictures\Penguins.jpg</sys:String>
  11.             <sys:String>C:\Users\Public\Pictures\Sample Pictures\Tulips.jpg</sys:String>
  12.         </x:Array>
  13.     </DockPanel.Resources>
  14.     <ScrollViewer>
  15.         <ListView x:Name="Images" ItemsSource="{StaticResource ImageLocations}" Background="Transparent">
  16.             <ListView.ItemTemplate>
  17.                 <DataTemplate>
  18.                     <Image Source="{Binding }" Height="50"/>
  19.                 </DataTemplate>
  20.             </ListView.ItemTemplate>
  21.         </ListView>
  22.     </ScrollViewer>
  23.     <Button Margin="20" Height="100">
  24.         <Grid>
  25.             <Image Source="{Binding SelectedValue, ElementName=Images}" />
  26.             <TextBlock Text="The Button" FontSize="18" VerticalAlignment="Center" HorizontalAlignment="Center" FontWeight="Bold">
  27.                 <TextBlock.Effect>
  28.                     <DropShadowEffect BlurRadius="18" Color="White" ShadowDepth="0" Opacity="1"/>
  29.                 </TextBlock.Effect>
  30.             </TextBlock>
  31.         </Grid>
  32.     </Button>
  33. </DockPanel>
In your case, you want your list to be 2 buttons...
Create a source that contains 2 pictures

Then, set this list to be the ItemsSource for the ListView displaying the options.

Then, set the ListView.ItemTemplate to display buttons with Images:

Expand|Select|Wrap|Line Numbers
  1. <StackPanel Background="Black">
  2.     <StackPanel.Resources>
  3.         <x:Array x:Key="ImageLocations"  Type="sys:String">
  4.             <sys:String>C:\Users\Public\Pictures\Sample Pictures\Desert.jpg</sys:String>
  5.             <sys:String>C:\Users\Public\Pictures\Sample Pictures\Koala.jpg</sys:String>
  6.         </x:Array>
  7.     </StackPanel.Resources>
  8.     <Image x:Name="theImage" Height="200" Source="{Binding SelectedValue, ElementName=Images}"/>
  9.     <ListView x:Name="Images" ItemsSource="{StaticResource ImageLocations}" 
  10.                 SelectedIndex="0" 
  11.                 Background="Transparent" 
  12.                 Margin="5,10"
  13.                 BorderThickness="0">
  14.         <ListView.Resources>
  15.             <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" />
  16.             <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" />
  17.         </ListView.Resources>
  18.         <ListView.ItemsPanel>
  19.             <ItemsPanelTemplate>
  20.                 <StackPanel Orientation="Horizontal" HorizontalAlignment="Center"></StackPanel>
  21.             </ItemsPanelTemplate>
  22.         </ListView.ItemsPanel>
  23.         <ListView.ItemTemplate>
  24.             <DataTemplate>
  25.                 <Button IsHitTestVisible="False" Height="50"  Width="50">
  26.                     <Image Source="{Binding}" Height="20"/>
  27.                 </Button>
  28.             </DataTemplate>
  29.         </ListView.ItemTemplate>
  30.     </ListView>
  31. </StackPanel>
-Frinny
Sep 19 '11 #3
All of these are best answer :)
Sep 20 '11 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Peter Moscatt | last post by:
I am having trouble understanding the methods for the Listbox from Tk. If I was to select at item in the list using a mouse click (have already created the bind event) - what method returns the...
2
by: farseer | last post by:
Hi, I have a combobox who's data source i set to an array of objects (call it MyObject). these objects have get properties: key, value, descr. i set ValueMember to "key", DisplayMember to...
2
by: Alpha | last post by:
How do I change the selected item in a listbox according to a Combox's selected item on the same form? The Combox is from a different table in the same dataset as the Listbox uses. Combox's...
2
by: OldNewbie | last post by:
Hello All I have a textbox control. I would like this text box to automatically update to contain the currently selected item located in a listbox which is on the same form. Do I need a...
3
by: rohith | last post by:
Hope someone can help with this. I am trying to add items to a drop down list from a database. I can do this fine by using the bind method. What I want to do also is to set the selected item to...
0
by: buran | last post by:
Dear ASP.NET Programmers, I have a datalist, which displays employee information. In selected item mode, the datalist displays detailed information and a button to switch to edit mode. What I...
3
by: Stephen Adam | last post by:
Hi there, I'm sure i'm missing something really simple here, all i want to do is get the value of the selected item in a list box. Even after much fiddling about last night I still could not get...
1
by: Karen Grube | last post by:
Hi! I'm using a standard server side ASP.Net listbox control on a web form. The page is basically various shades of green. The listbox itself has a pale green background and forest green text...
4
by: juststarter | last post by:
Hello, I have an aspx file where i've put a placeholder element. On load (page_load) i create dynamically an html table which contains a checkbox and a radiobuttonlist in each tablerow . The...
6
by: Smokey Grindle | last post by:
Say I have a combo box with the following simple object Public class MyObject public ID as integer public Name as string public overrides sub ToString() as string return name end sub end...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.