AutoCompleteBox绑定到web服务

本文关键字:服务 web 绑定 AutoCompleteBox | 更新日期: 2023-09-27 18:05:19

第一次在silverlight上工作,想要绑定AutoCompleteBox(通过web服务)与图像拇指和图像标题,将通过图像标题搜索,我可以做绑定图像标题

工作过程
  1. 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>() ;
    }
    

  2. xaml

        <sdk:AutoCompleteBox x:Name="searchText"
                  Populating="searchText_Populating" />
                </StackPanel>
    
  3. 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(); 
    }
    

想要绑定图像的url为图像拇指和图像标题,我应用以下过程

<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的数据模板,我可以使用字典吗?如果是,那么我如何将它绑定到数据模板

AutoCompleteBox绑定到web服务

ItemsControl文档应该为您提供了入门所需的几乎所有内容。

在您的例子中,您最终将使用Binding语法来设置ItemTemplate中图像的ImageSource。为此,您需要使用值转换器将字符串转换为图像可以使用的内容。

<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();
    }
}