2011-12-18 50 views
1

無法在查找對象上設置圖像源Uri。 Stackpanel包含兩個孩子文本框和圖像控件。無法在Silverlight 4中設置圖像源

private void TextBox_LostFocus(object sender, RoutedEventArgs e) 
{ 
    var textBox = (TextBox)sender; 
    textBox.Style = Application.Current.Resources["TextBoxNormal"] as Style; 
    textBox.FontSize = 15; 
    textBox.Foreground = new SolidColorBrush(Colors.Gray);  

    var stackpanel = textBox.Parent as StackPanel; 
    if (stackpanel == null)return; 
    var img = stackpanel.Children.Where(a => a is Image).FirstOrDefault(); 

    if (textBox.Text != "") 
    { 
    //I was trying set Uri as mention below, but there is Nothing like "Image.Source" 
    //image.Source = new BitmapImage(new Uri("/Images/Others/TickRight.png", UriKind.RelativeOrAbsolute)); 

    } 

回答

1

需要投IMG作爲圖像

private void TextBox_LostFocus(object sender, RoutedEventArgs e) 
{ 
    var stackpanel = textBox.Parent as StackPanel; 
    if (stackpanel == null)return; 
    var img = stackpanel.Children.Where(a => a is Image).FirstOrDefault() as Image; 
} 
+0

變種IMG = stackpanel.Children.Where(A => a是圖片).FirstOrDefault()作爲圖像; – Faizan 2011-12-19 17:28:36

1

所以我會建議在這裏進行故障排除。

if (textBox.Text != "") 
    { 
     BitmapImage picture = new BitmapImage(new Uri("/Images/Others/TickRight.png", UriKind.RelativeOrAbsolute)); 
     picture.ImageFailed += (o, e) => System.Threading.SynchronizationContext.Current.Send((oo) => System.Windows.Browser.HtmlPage.Window.Alert("Image failed: " + e.ErrorException), null); 
     picture.ImageOpened += (o, e) => System.Threading.SynchronizationContext.Current.Send((oo) => System.Windows.Browser.HtmlPage.Window.Alert("Image opened: " + e.OriginalSource), null); 

     image.Source = picture; 
    } 

// BANG ..你的錯誤應該是裸體!