locked
image in datatemplate does not work!!! RRS feed

  • Question

  • hi 

    i have a gridview that when i click header headertemplate should change.

    this is my code:

     BitmapImage o = new BitmapImage();
                            o.BeginInit();
                            o.UriSource = new Uri("images\\Down.png", UriKind.RelativeOrAbsolute);
                            o.EndInit();
    
                            FrameworkElementFactory t = new FrameworkElementFactory(typeof(TextBlock));
                            t.SetValue(TextBlock.TextProperty, "aaaaaa");
    
                            FrameworkElementFactory img = new FrameworkElementFactory(typeof(Image));
                            img.SetValue(Image.SourceProperty, o);
                            img.SetValue(Image.StretchProperty, Stretch.None);
    
    
                            FrameworkElementFactory fef = new FrameworkElementFactory(typeof(StackPanel));
                            fef.AppendChild(t);
                            fef.AppendChild(img);
    
                            fef.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal);
    
                            DataTemplate d = new DataTemplate();
    
                            d.VisualTree = fef;
    
                            headerClicked.Column.HeaderTemplate = d;

    textblock works but image doesn't show anything!!!

    what is problem?


    hamed

    Friday, June 29, 2012 8:22 AM

Answers

  • Hi hmd810,

    I think you need set the o.CacheOption property, such as:

    o.CacheOption = BitmapCacheOption.OnLoad;

    Best regards,


    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by hmd810 Wednesday, July 4, 2012 7:47 AM
    Monday, July 2, 2012 9:27 AM

All replies

  • Hi,

    Have you checked if the relative URI is right? Does the BitmapImage really contain your png?

    Friday, June 29, 2012 8:25 AM
  • yes  relative URI is right .

    hamed

    Friday, June 29, 2012 8:26 AM
  • hi,

    try setting the property of image . set property "BuildAction" to "Resource".  it is accessible in "Properties" window.

    if it is in project folder.


    Thanks & Regards dhampall


    Friday, June 29, 2012 8:43 AM
  • Hi,

    I think the way of relative URI may cause some issues. Ensure that the obj o has the value. 

    http://sankarblog.wordpress.com/2012/03/21/relative-path-in-c/ refer this if it may use for you...


    sankar

    Friday, June 29, 2012 9:40 AM
  • i realy confuse.

    if i change my like here image will show picture but i still have problem

       BitmapImage o = new BitmapImage();
                            o.BeginInit();
                            o.UriSource = new Uri("images\\Down.png", UriKind.RelativeOrAbsolute);
                            o.EndInit();
                            
    
    
                            Image f = new Image();
                            f.BeginInit();
                            f.Source = o;
                            f.EndInit();
                            mygrid.Children.Add(f);
    
                            FrameworkElementFactory t = new FrameworkElementFactory(typeof(TextBlock));
                            t.SetValue(TextBlock.TextProperty, "aaaaaa");
    
                            FrameworkElementFactory img = new FrameworkElementFactory(typeof(Image));
                            img.SetValue(Image.SourceProperty, o);
                            img.SetValue(Image.StretchProperty, Stretch.None);
                       
                            
    
                            FrameworkElementFactory fef = new FrameworkElementFactory(typeof(StackPanel));
                            fef.AppendChild(t);
                            fef.AppendChild(img);
    
                            fef.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal);
    
                            DataTemplate d = new DataTemplate();
    
                            d.VisualTree = fef;
    
                            headerClicked.Column.HeaderTemplate = d;

    here i make an image element and add to my main grid both new image element and header template show picture . what's going on here?


    hamed

    Friday, June 29, 2012 10:23 AM
  • Hi hmd810,

    I think you need set the o.CacheOption property, such as:

    o.CacheOption = BitmapCacheOption.OnLoad;

    Best regards,


    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by hmd810 Wednesday, July 4, 2012 7:47 AM
    Monday, July 2, 2012 9:27 AM