第一次使用silverlight,想用图像拇指和图像标题绑定AutoCompleteBox(通过web服务),将搜索图像标题,我能够做绑定图像标题
工作流程
网络服务
[WebMethod]
public List<imagedata> Searchimage(string s)
{
var db = new PetaPoco.Database("sales");
var list = from search in db.Query<imagedata>("select * from imagedata").ToList() where search.imgname.Contains(s) select search;
return list.ToList<imagedata>() ;
}
XAML
<sdk:AutoCompleteBox x:Name="searchText"
Populating="searchText_Populating" />
</StackPanel>
的.cs
private void searchText_Populating(object sender, PopulatingEventArgs e)
{
mySoapClient proxy = new mySoapClient();
proxy.SearchimageCompleted +=
new EventHandler<SearchimageCompletedEventArgs>(proxy_ImageGetCompleted);
proxy.SearchimageAsync(searchText.Text);
}
void proxy_ImageGetCompleted(object sender, SearchimageCompletedEventArgs e)
{
var searchResults = new List<imagedata>(e.Result);
var data = new List<string>();
foreach (var x in searchResults)
{
data.Add(x.imgname);
}
searchText.ItemsSource = data;
searchText.PopulateComplete();
}
想要为图像缩略图和图像标题绑定图像网址,我应用以下过程
<sdk:AutoCompleteBox x:Name="searchText" Populating="searchText_Populating">
<sdk:AutoCompleteBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image x:Name="image1" Height="100" Width="100" Canvas.Left="0" Canvas.Top="0"></Image>
<TextBlock VerticalAlignment="Center" Margin="10 0 0 0" />
</StackPanel>
</DataTemplate>
</sdk:AutoCompleteBox.ItemTemplate>
如何将集合绑定到autocompletebox的datatemplate,我可以使用字典吗?如果是,那么我如何将其绑定到数据模板
答案 0 :(得分:0)
ItemsControl documentation几乎可以为您提供入门所需的一切。
在您的情况下,您最终将使用Binding语法在ItemTemplate中设置Image的ImageSource。为此,您需要使用值转换器将字符串转换为Image可以使用的字符串。
<sdk:AutoCompleteBox x:Name="searchText" Populating="searchText_Populating">
<sdk:AutoCompleteBox.Resources>
<local:ImageSourceConverter x:Key="ImageSourceConverter" />
</sdk:AutoCompleteBox.Resources>
...
<Image x:Name="image1"
Height="100"
Width="100"
Canvas.Left="0"
Canvas.Top="0"
ImageSource="{Binding Converter={StaticResource ImageSourceConverter}}">
</Image>
...
转换器:
public class ImageSourceConverter : System.Windows.Data.IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
string source = value as string;
if (source != null)
{
return new System.Windows.Media.Imaging.BitmapImage
{
UriSource = new Uri(source, UriKind.Absolute)
};
}
return source;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}