views:

170

answers:

1

Hi I have the following code:

<ListBox x:Name="foldersListBox" Grid.Column="0" MouseLeftButtonUp="foldersListBox_MouseLeftButtonUp" 


                             BorderThickness="0"  Height="AUTO" 
                             ScrollViewer.HorizontalScrollBarVisibility="Disabled"
                             ScrollViewer.VerticalScrollBarVisibility="Disabled">
                        <DataTemplate>
                            <Border BorderBrush="LightGray"
                            BorderThickness="2"
                            CornerRadius="4">
                                <Image x:Name="folderImage" Width="70" Height="70" Margin="3" />
                            </Border>
                        </DataTemplate>
</ListBox>

Now when I am trying to access folderImage from code behind. I can use the loaded event and typecast the sender as Image type , but I dont want that way,because I want to bind the image source during runtime binding . So even if we will try on loaded event , thatz not going to help as the control wont be loaded .

Help plz.

Thanks, Subhen

+1  A: 

There is quite a bit of detail missing from your question but I'm going to stab at answering anyway. Its highly unlike to answer your question but it might help you see what detail you need to add the question to guide the answers. In turn this answer can be refined. Some iterations down the road you may actually arrive at an answer.

I'm gonna guess that you are binding to a set of object that represent "Folders" but you want programmatically modify the image being presented depending on the state of each object, for example some FolderType property.

On solution to this is to use a value converter, if your images are from a finite set.

public class FolderToImageConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        Folder folder = value as Folder;
        ImageSource result;
        // Logic to determine which ImageSource to use for a folder.
        return result;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Now take a look at this XAML:-

<Grid x:Name="LayoutRoot">
    <Grid.Resources>
       <local:FolderToImageConverter x:Key="ImageConverter" />
    </Grid.Resources>
    <ListBox x:Name="foldersListBox">
        <ListBox.ItemTemplate>
             <DataTemplate> 
                <Border BorderBrush="LightGray" BorderThickness="2" CornerRadius="4"> 
                  <Image Source="{Binding Converter={StaticResource ImageConverter}}" Width="70" Height="70" Margin="3" /> 
                </Border> 
             </DataTemplate> 
        </ListBox.ItemTemplate>
    </ListBox> 
</Grid>

Once you have bound your collection of Folder objects to the ListBox ItemsSource it will display a set of images using the converter to transform the Folder object to the correct ImageSource instance.

AnthonyWJones
Hi , Here is what I want, Initially My Listbox will have different images. Once I will click on one of images (selectedItem) , the selected Item should change to another image.
Subhen
@Subhen: "Different Images" elaborate? This set of images is determined by a URL present in the source data perhaps? Or there a finite set of possible images listed statically from which one is chosen based of data in the data object? Similarly the "another image" how is that determined?
AnthonyWJones