Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
XML
MainWindow.xaml

<Window.Resources>
        <DataTemplate x:Key="movieImage">
            <StackPanel>
                <Image Stretch="Fill">
                    <Image.Source>
                        <BitmapImage UriSource="{Binding Images}"/>
                    </Image.Source>
                </Image>
            </StackPanel>
        </DataTemplate>
    </Window.Resources>
    <Grid>
        <!--<TextBlock Text="Choose Movie" FontSize="15"/>-->
        <StackPanel>
            <ListBox Width="200" Height="100">
                <ListBoxItem>
                    <Image Source="{Binding Images}"></Image>
                </ListBoxItem>
            </ListBox>
        </StackPanel>
    </Grid>

//MainWindow.xaml.cs
public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.move = new Movies();

            this.DataContext = this.move;
        }

        public Movies move { get; set; }
    }

//Movies.cs
public class Movies
    {
        public Movies()
        {
            define = new Define();
            Book();
        }

        public Define define { get; set; }

        private void Book()
        {
            this.define.Movie = new ObservableCollection<MovieModel>();

            this.define.Movie.Add(new MovieModel() {Images = new Uri(@"\Terminator.jpg",UriKind.RelativeOrAbsolute) });
        }

    }

//MovieModel.cs
public class MovieModel
    {
        public string MovieName { get; set; }

        public Uri Images { get; set; }
    }

//Define.cs
public class Define
    {
        public ObservableCollection<MovieModel> Movie { get; set; }

        public ObservableCollection<Theaters> Theater { get; set; }
    }

I try the above coding, my problem is doesnt bind the image inside the listbox, i cant understand why it should not bind, please help to solve this problem, thanks in advance.
Posted
Updated 13-Oct-15 18:48pm
v2

1 solution

C#
this.move = new Movies();
this.DataContext = this.move;  // you're assigning newly referenced object as DataContext, which hasn't any value

Please refer this,
http://www.progware.org/Blog/post/ListBoxes-with-DataBound-Images-in-WPF.aspx[^]
http://stackoverflow.com/questions/21788855/binding-an-image-in-wpf-mvvm[^]
http://stackoverflow.com/questions/2199447/binding-an-image-in-wpf[^]
Simple Way to Bind an Image Class as Source to Image Control[^]
And this one[^] too.

-KR
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900