Binding 如何将图像绑定到WPF中的ListBoxItem?
我尝试将图像绑定到我创建的列表框中,但它不起作用,any1可以帮助解决我的问题,以便我可以实际将图像绑定到“ListBoxItem”中,如下面的代码所示 代码隐藏:Binding 如何将图像绑定到WPF中的ListBoxItem?,binding,Binding,我尝试将图像绑定到我创建的列表框中,但它不起作用,any1可以帮助解决我的问题,以便我可以实际将图像绑定到“ListBoxItem”中,如下面的代码所示 代码隐藏: public object ImageSource { get { Image finalImage = new Image(); BitmapImage logo = new BitmapImage(); log
public object ImageSource
{
get
{
Image finalImage = new Image();
BitmapImage logo = new BitmapImage();
logo.BeginInit();
logo.UriSource = new Uri(@"/images/food1.bmp", UriKind.Relative);
logo.EndInit();
finalImage.Source = logo;
return logo;
}
}
XAML:
可能是imagesource有问题。检查是否解决了您的问题。这里有一个小问题。如果我想像在上面的代码中那样将图像绑定到这一行“”,我可以使用哪种方法来绑定?我尝试了“var uriSource=new Uri(@)/test;images/food1.bmp”,UriKind.Relative);img.Source=new BitmapImage(uriSource);“我做得对吗?但不知怎的,图像没有显示出来。
<ListBox Grid.ColumnSpan="2">
<ListBox.Resources>
<Style TargetType="{x:Type Image}">
<Setter Property="Width" Value="100"/>
<Setter Property="Height" Value="100"/>
<EventSetter Event="MouseLeftButtonDown" Handler="DragImage"/>
</Style>
</ListBox.Resources>
<ListBoxItem>
<Image Source="{Binding Path=ImageSource}" Width="197" Height="120" Margin="0,0,0,0" />
</ListBoxItem>
</ListBox>