C# 如何以编程方式(WPF)将图像放入进度条
我对WPF完全陌生,我需要一些帮助(以编程方式)将图像放入进度栏 我发现了这个(它是有效的):C# 如何以编程方式(WPF)将图像放入进度条,c#,wpf,xaml,programmatically-created,C#,Wpf,Xaml,Programmatically Created,我对WPF完全陌生,我需要一些帮助(以编程方式)将图像放入进度栏 我发现了这个(它是有效的): 我只是需要一些帮助来将XAML代码转换为C代码。这将帮助您开始: FrameworkElementFactory grid = new FrameworkElementFactory(typeof(Grid)); FrameworkElementFactory image = new FrameworkElementFactory(typeof(Image)); image.Name = "PA
我只是需要一些帮助来将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;
什么是常规代码?@nvartak我指的是C代码。我需要以编程方式在C#中再现XAML代码的效果。我正在制作一个带有多个主题的自定义进度条(您可以在运行时通过右键单击进度条在ContextMenu中更改当前主题),我不想做很多样式。
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;