如何分配样式以通过不在XAML中的C#代码进行控制 [英] how to assign style to control through C# code not in xaml

查看:69
本文介绍了如何分配样式以通过不在XAML中的C#代码进行控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的进度栏中,我已将样式分配给了"BorderBrsuh"和前景",现在在XAML中,我想分配相同的内容,但要使用"C#代码"那么该怎么做?????

In below progress bar I have assigned style to "BorderBrsuh" and "Foreground", now this is in XAML I want to assign same things but with "C# code" so how to do that?????

 


<ProgressBar Background="White" BorderBrush="{StaticResource BackgroundNormal}" Canvas.Left="1" Canvas.Top="29" Foreground="{StaticResource BackgroundNormal}" Height="21" LargeChange="1" MaxWidth="Infinity" Maximum="100" Minimum="0" Name="pgBurnStatus" SmallChange="0.1" Width="279" ToolTip="Burn process progress" />

推荐答案

为什么不在XAML中设置样式?

Why not style it in XAML ?

无论如何,您可以这样做

Anyways, You could do like this 


    Style setProgressBarStyle(ProgressBar control, string Name)
    {
      Style style = new Style();
      style.TargetType = control.GetType();

      Setter s9 = new Setter(ProgressBar.NameProperty, Name);
      Setter s1 = new Setter(ProgressBar.BorderBrushProperty, new SolidColorBrush(Colors.Black));
      Setter s2 = new Setter(ProgressBar.BackgroundProperty, new SolidColorBrush(Colors.White));
      Setter s3 = new Setter(ProgressBar.HeightProperty, 21);
      Setter s8 = new Setter(ProgressBar.WidthProperty, 279);
      Setter s4 = new Setter(ProgressBar.ToolTipProperty, "Burn process Progress");
      Setter s5 = new Setter(ProgressBar.MaximumProperty, 100);
      Setter s6 = new Setter(ProgressBar.MinimumProperty, 0);
      Setter s7 = new Setter(ProgressBar.SmallChangeProperty, 0.1);      

      return style;
    }


这篇关于如何分配样式以通过不在XAML中的C#代码进行控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆