0

I'm completely new to WPF and I need some help putting an image in my progress bar (programmatically).

I found this (and it works):

<ProgressBar.Template>
    <ControlTemplate>
        <Grid>
            <Image Name="PART_Track" Source="MyImage" Stretch="Fill"/>
            <Rectangle Name="PART_Indicator" Fill="BlanchedAlmond" HorizontalAlignment="Left"/>
        </Grid>
    </ControlTemplate>
</ProgressBar.Template>

I just need some help converting that XAML code to C# code.

soulblazer
  • 1,178
  • 7
  • 20
  • 30
  • What is regular code? – Nikhil Vartak Nov 26 '15 at 16:47
  • @nvartak I meant the C# code. I need to reproduce the effect from the XAML code programmatically, in C#. I'm making a custom progress bar with several themes (you can change the current theme in runtime with a ContextMenu by right clicking the progress bar) and I don't want to do lots of styles. – soulblazer Nov 26 '15 at 16:50

1 Answers1

1

This will get you started :

FrameworkElementFactory grid = new FrameworkElementFactory(typeof(Grid));

FrameworkElementFactory image = new FrameworkElementFactory(typeof(Image));
image.Name = "PART_Track";
ImageSource source = new BitmapImage(...); // create it
image.SetValue(Image.SourceProperty, source);
image.SetValue(Image.StretchProperty, Stretch.Fill);

grid.AppendChild(image);

FrameworkElementFactory rectangle = new FrameworkElementFactory(typeof(Rectangle));
rectangle.Name = "PART_Indicator";
rectangle.SetValue(Rectangle.FillProperty, new SolidColorBrush(Colors.BlanchedAlmond));
rectangle.SetValue(Rectangle.HorizontalAlignmentProperty, HorizontalAlignment.Left);

grid.AppendChild(rectangle);

    ControlTemplate ct = new ControlTemplate(typeof(ProgressBar));
ct.VisualTree = grid;

MyProgressBar1.Template = ct;
AnjumSKhan
  • 9,647
  • 1
  • 26
  • 38