Не удается установить источник изображения в Silverlight 4 - PullRequest
1 голос
/ 18 декабря 2011

Невозможно установить источник изображения Uri при поиске объекта.Stackpanel содержит два дочерних Textbox и контроль изображения.

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

  }

Ответы [ 2 ]

1 голос
/ 19 декабря 2011

Вам нужно сыграть 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;
 }
1 голос
/ 19 декабря 2011

Так что я бы предложил здесь устранение неисправностей.

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 .. ваша ошибка должна быть голой!

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...