0

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

工作流程

  1. 网络服务

    [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. xml

        <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>

如何将集合绑定到自动完成框的数据模板,我可以为此使用字典吗?如果是,那么我如何将其绑定到数据模板

4

1 回答 1

0

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

在您的情况下,您最终将使用 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();
    }
}
于 2013-03-24T09:18:09.920 回答