views:

138

answers:

1

I have a ListBox populated with a list from ImageDomainService (RIA Services). I want to select one image from the ListBox and show the bigger version of the image beside it. Images are stored separate in /images/ folder How do I get the ImageName binding from ListBox to the url string in code behind like below?

void AlbumView_Loaded(object sender, RoutedEventArgs e)
{
    ImageDomainContext ctx = new ImageDomainContext();
    listBoxImages.ItemsSource = ctx.Images;
    ctx.Load(ctx.GetImagesListQuery());
}  

XAML:

<ListBox x:Name="listBoxImages" ItemsSource="{Binding}"
    SelectionChanged="ListBox_SelectionChanged">  
    <ListBox.ItemTemplate>  
        <DataTemplate>  
            <TextBlock x:Name="ImageNameTextBox" Text="{Binding ImageName}" />  
            <TextBlock Text="{Binding ImageDescription}" />  
        </DataTemplate>  
    </ListBox.ItemTemplate>  
</ListBox>  

Event Handler:

private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    Image _image = new Image();
    BitmapImage bi = new BitmapImage();

    // string url = ???????????
    bi.UriSource = new Uri(string.Format("images/{0}", url), UriKind.Relative);

    _image.Source = bi;

    _image.Width = 500;
    _image.Height = 300;

    bigImageBorder.Child = _image;
}  
+1  A: 

Why not just use the SelectedItem property instead?:

// Put the class that you're binding to here...
MyClass instance = listBoxImages.SelectedItem as MyClass;
string url = instance.ImageName; // url is an odd variable name for this...
bi.UriSource = new Uri(string.Format("images/{0}", url), UriKind.Relative);  

Also, you could potentially make a IValueConverter for the selected item that did this directly, so you could bind your other image source directly to the selected item, without any code behind.

Reed Copsey
Thank you! I forgot to bind it to my Image class and not the (wrong)ListBox Control class.Also i did your IValueConverter tip and bound that earlier to find what was wrong. Now I got 2 working ways...
Niike2