WPF ProgressBar 样式

希望写个ProgressBar的样式,在progress value不同的时候显示不同的颜色的progress bar

1.写转换器

public class ProgressBarValueConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            double v = (double)value;

            string imageUri = "../Images/progress bar_1.png";

            if (v > 80)
            {
                imageUri = "../Images/progress bar_3.png";
            }
            else if (v > 50)
            {
                imageUri = "../Images/progress bar_2.png";
            }

            BitmapImage img = new BitmapImage(new Uri(imageUri, UriKind.Relative));

            return img;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

2.创建样式

    <c:ProgressBarValueConverter x:Key="pbConverter"/>
    <Style x:Key="ProgressBarStyle" TargetType="{x:Type ProgressBar}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate>
                    <Grid>
                        <Image Name="PART_Track" Source="../Images/progress bar.png" HorizontalAlignment="Left" Stretch="Fill"/>
                        <Image Name="PART_Indicator" Source="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Value, Converter={StaticResource pbConverter}}"
                                   HorizontalAlignment="Left" Stretch="Fill"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

3.设置progress bar的style

 

progress bar.png: 

progress bar_1.png: 

progress bar_2.png:

progress bar_3.png:

 

------------------------------------------------------------------

 

如果仅仅是希望创建一个扁平化的progress bar,那么连转换器都可以不用写

<Style x:Key="FlatProgressBar" TargetType="{x:Type ProgressBar}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate>
                    <Grid>
                        <Image Name="PART_Track" Source="../Images/Flat Progress Bar.png" HorizontalAlignment="Left" Stretch="Fill"/>
                        <Image Name="PART_Indicator" Source="../Images/Flat Progress Bar_1.png" HorizontalAlignment="Left" Stretch="Fill"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
Flat Progress Bar.png:

Flat Progress Bar_1.png:

 

转载于:https://www.cnblogs.com/AlvinLiang/p/5513096.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值