在Wpf中绑定图像 [英] Binding Image in Wpf
问题描述
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.
推荐答案
this.move = new Movies();
this.DataContext = this.move; // you're assigning newly referenced object as DataContext, which hasn't any value
请参考这个,
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 [ ^ ]
将图像类绑定为图像控制源的简单方法 [ ^ ]
这个一个 [ ^ ]。
-KR
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
这篇关于在Wpf中绑定图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!