2013-03-23 50 views
0

第一次工作的Silverlight,想與圖像拇指和圖片標題綁定AutoCompleteBox(通過Web服務),將通過圖像標題搜索,我能夠做綁定圖片標題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(); 
    
    } 
    

想爲圖像拇指和圖像都標題圖片的網址綁定,我申請以下過程

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

我怎麼能收集綁定到DataTemplate中的autocompletebox,我可以使用字典嗎?如果是,那麼我如何將它綁定到數據模板

回答

0

ItemsControl documentation應該給你幾乎所有你需要的東西開始。

在你的情況下,你最終將使用綁定語法來在你的ItemTemplate中設置圖像的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(); 
    } 
}