【发布时间】:2018-02-25 13:21:59
【问题描述】:
我必须使用GridViewItem 样式的图像,每个GridViewItem 代表保存在 SQLite 数据库中的对象,当我将新对象保存到数据库中时,我必须从组合框中选择它使用的图像。
这是 Combobox 的定义:
<ComboBox x:Name="UriImage" Width="200" RelativePanel.Below="ActiveToggle">
<ComboBoxItem Content="Element1" x:Name="element1" IsSelected="True"/>
<ComboBoxItem Content="Element2" x:Name="element2"/>
<ComboBoxItem Content="Element3" x:Name="element3"/>
</ComboBox>
然后我创建了它以将图像的相应 URI 与每个 ComboBoxItem 匹配
string ImageUriString="";
if (UriImage.SelectedValuePath == "Element1")
ImageUriString = "ms-appx:///Assets/Orchid_2_FF.png";
else if (UriImage.SelectedValuePath == "Element2")
ImageUriString = "ms-appx:///Assets/sapphire_orchid_FF.png";
else if (UriImage.SelectedValuePath == "Element3")
ImageUriString = "ms-appx:///Assets/yellow-cowslip-orchid_FF.png";
Uri UriString = new Uri(ImageUriString, UriKind.RelativeOrAbsolute);
然后我将Uri字段添加到代表对象的类中
当我尝试保存新对象时出现此错误:Don't know how to read System.Uri
要在 Gridview 中加载对象,我使用这个:
private async void ReadAllSystemList_Loaded(object sender, RoutedEventArgs e)
{
ReadAllSystemsList dbSystems = new ReadAllSystemsList();
DB_FFSystems = await dbSystems.GetAllFFSystems(); //get all DB Systems
listBoxObj.ItemsSource = DB_FFSystems.OrderByDescending(i => i.ID).ToList();
}
和来自 Xaml 的数据绑定。
<Image Width="350" Height="200" x:Name="ElementImage" Source="{Binding ImageUri}">
我曾考虑将 URI 保存为字符串,但如何在加载 Gridview 时将字符串转换为 Uri?
【问题讨论】:
-
您好 ZampTom,您可以使用转换器并仅绑定到字符串。但我认为绑定到 Imagesource 的字符串仍然有效?