请帮忙。我在ResourceDictionary<DrawingImage x:Key="MyDrawingImage">
中有一个XAML图像。我如何将它绑定到ListBox.ItemTemplate>使用MVVM ?List Item有文字和图片
public class MyItemViewModel {
public string TextToDisplay { get; set; }
public string? ImageToDisplay { get; set;} ??????
}
问题是,从资源字典绑定图像的最优雅的方式是什么?
<ListBox.ItemTemplate>
<DataTemplate>
<Border CornerRadius="10">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="3*" />
<RowDefinition Height="2*" />
</Grid.RowDefinitions>
<Image Source="{Binding ImageToDisplay}" ???????? />
<TextBlock Grid.Row="1"
Text="{Binding TextToDisplay}"/>
</Grid>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
我终于找到了答案。也许这对某人会有用:
public class DsItemViewModel {
public DsItemViewModel(string name, string? drawingImageResourceKey = null) {
Name = name;
if (drawingImageResourceKey != null)
Image = Application.Current.FindResource(drawingImageResourceKey) as DrawingImage;
}
public string Name { get; }
public DrawingImage? Image { get; }
}