如何以编程方式将图像放入进度条 (WPF)

How to put an image in a progress bar programmatically (WPF)

我是 WPF 的新手,我需要一些帮助(以编程方式)将图像放入我的进度条中。

我找到了这个(并且有效):

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

我只需要一些帮助将 XAML 代码转换为 C# 代码。

这将帮助您入门:

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;