无法在Silverlight 4中设置图像源

时间:2011-12-18 09:38:11

标签: silverlight-4.0

无法在查找对象上设置图像源Uri。 Stackpanel包含两个子文本框和图像控件。

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 个答案:

答案 0 :(得分:1)

您需要将img转换为Image

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