mu_sa
mu_sa

Reputation: 2725

Does not exist in the current context error

When i try to bind my Image in XAML to bitmapImage object in the code behind it gives me 'Does not exist in the current context' error.

Code

BitmapImage bitmapImage = new BitmapImage();
PhotoSource.Source = bitmapImage;
ObservableCollection<BitmapImage> Photos = new ObservableCollection<BitmapImage>();
PhotoList.ItemsSource = Photos;

XAML

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,5,12,-10">
        <ProgressBar x:Name="progressBar" HorizontalAlignment="Left" Height="40" Margin="0,0,0,0" VerticalAlignment="Top" Width="436" Visibility="Collapsed" IsIndeterminate="True"/>
        <ListBox x:Name="PhotoList" 
                 toolkit:TiltEffect.IsTiltEnabled="True"
                 SelectionChanged="PhotoList_SelectionChange"
                 HorizontalAlignment="Left" Height="500" Margin="0,40,0,0" VerticalAlignment="Top" Width="450">
            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <toolkit:WrapPanel 
                        HorizontalAlignment="Left" 
                        Margin="0,0,0,0" 
                        VerticalAlignment="Top" 
                    />
                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Margin="5">
                        <StackPanel Orientation="Vertical">
                            **<Image delay:LowProfileImageLoader.UriSource="{Binding PhotoSource}" Width="99" Height="80"/>**
                        </StackPanel>
                    </StackPanel>

                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>

Upvotes: 1

Views: 5147

Answers (2)

Mateusz Rogulski
Mateusz Rogulski

Reputation: 7445

You need to:

  • Create some class which you want to bind to your. For example:

    private class Photo {
        public string PhotoSource {get; set;}
    }
    
  • Create a collection which you want to bind. For example, List<Photo> Photos = new List<Photo>();

  • Add some data to your list. For example, Photos.Add(new Photo { PhotoSource = yourBitmap });
  • Bind it to your list. PhotoList.ItemsSource = Photos;

Upvotes: 2

CKII
CKII

Reputation: 1486

First, make sure PhotoSource is a public property, since WPF doesn't recognize anything else.

Second, make sure you set your DataContext property properly. If the property is a part of the window's code behind, you can set the DataContext to the window, by setting the line:

DataContext="{Binding RelativeSource={RelativeSource self}}"

in the window declaration in the xaml, so it would look something like this:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525"
    DataContext="{Binding RelativeSource={RelativeSource self}}">
  <!-- Your Code here -->
</window>

Upvotes: 2

Related Questions