I am new to WPF, here I am making a jigsaw game but encountering a problem: I just don't know how to let a image show in a Label in pure code (in XAML, I can do it.), I have searched google a lot but seems that no releated articles regarding this topic. Anyone can help me on this? thx.
My Code Sample:
Label lbl = new Label();
lbl.Width = 120;
lbl.Height = 120;
lbl.Background = new SolidColorBrush(Colors.YellowGreen);
BitmapImage myImageSource = new BitmapImage();
myImageSource.BeginInit();
myImageSource.UriSource = new Uri("Images/test.png",UriKind.Relative);
myImageSource.EndInit();
lbl.Background.SetValue(ImageBrush.ImageSourceProperty, myImageSource);
myGridContainer.Children.Add(lbl);
well, this code sample can't work. why ? any comment or answer from you will be appreciated , thx.
lbl.Background = new ImageBrush()instead. Getting a good WPF book like 'Programming WPF' by IanGriffith, Chris Sells.. will help you code faster.