C#WPF等级控制类似于Wifi信号指示器

C#WPF等级控制类似于Wifi信号指示器,第1张

概述我已经搜索了很多 WPF等级控制simliar到这个图像中的wifi信号强度指示器,但我找不到一个 我试着自己做,这是结果:) <Grid> <Rectangle Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="58" Margin="90,114,0,0" Stroke="Black" VerticalAlignment="Top" 我已经搜索了很多 WPF等级控制simliar到这个图像中的wifi信号强度指示器,但我找不到一个

我试着自己做,这是结果:)

<GrID>    <Rectangle Fill="#FFF4F4F5" HorizontalAlignment="left" Height="58" margin="90,114,0" stroke="Black" VerticalAlignment="top" WIDth="22"/>    <Rectangle Fill="#FFF4F4F5" HorizontalAlignment="left" Height="74" margin="117,98,0" stroke="Black" VerticalAlignment="top" WIDth="22"/>    <Rectangle Fill="#FFF4F4F5" HorizontalAlignment="left" Height="93" margin="144,79,0" stroke="Black" VerticalAlignment="top" WIDth="22"/>    <Rectangle Fill="#FFF4F4F5" HorizontalAlignment="left" Height="111" margin="171,61,0" stroke="Black" VerticalAlignment="top" WIDth="22"/>    <Rectangle Fill="#FFF4F4F5" HorizontalAlignment="left" Height="124" margin="198,48,0" stroke="Black" VerticalAlignment="top" WIDth="22"/></GrID>

我需要根据评级值更改矩形颜色,例如:

<l:UserControl1 ratingValue="3" />

这将为前三个矩形着色

任何人都可以帮我这样做,或找到类似的控件吗?

解决方法 您可以创建一个IValueConverter来更改矩形的颜色

这是一个非常快速(粗略)的例子:

XAML:

<GrID Background="DarkBlue"  >    <GrID.Resources>        <local:ratingConverter x:Key="ratingConverter" OnBrush="#FFFFFFFF" OffBrush="#50FFFFFF" />        <Style targettype="Rectangle">            <Setter Property="HorizontalAlignment" Value="left" />            <Setter Property="VerticalAlignment" Value="Bottom" />            <Setter Property="margin" Value="5,0" />        </Style>    </GrID.Resources>    <StackPanel OrIEntation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">        <Rectangle WIDth="5" Height="5" Fill="{Binding ratingValue,Converter={StaticResource ratingConverter},ConverterParameter=1}"/>        <Rectangle WIDth="5" Height="10" Fill="{Binding ratingValue,ConverterParameter=2}"/>        <Rectangle WIDth="5" Height="15" Fill="{Binding ratingValue,ConverterParameter=3}"/>        <Rectangle WIDth="5" Height="20" Fill="{Binding ratingValue,ConverterParameter=4}"/>        <Rectangle WIDth="5" Height="25" Fill="{Binding ratingValue,ConverterParameter=5}"/>    </StackPanel></GrID>

码:

namespace WpfApplication14{    /// <summary>    /// Interaction logic for UserControl1.xaml    /// </summary>    public partial class UserControl1 : UserControl    {        public UserControl1()        {            InitializeComponent();            DataContext = this;        }        public int ratingValue        {            get { return (int)GetValue(ratingValueProperty); }            set { SetValue(ratingValueProperty,value); }        }        // Using a DependencyProperty as the backing store for ratingValue.  This enables animation,styling,binding,etc...        public static Readonly DependencyProperty ratingValueProperty =            DependencyProperty.Register("ratingValue",typeof(int),typeof(UserControl1),new UIPropertyMetadata(0));    }    public class ratingConverter : IValueConverter    {        public Brush OnBrush { get; set; }        public Brush OffBrush { get; set; }        public object Convert(object value,Type targettype,object parameter,System.Globalization.CultureInfo culture)        {            int rating = 0;            int number = 0;            if (int.TryParse(value.ToString(),out rating) && int.TryParse(parameter.ToString(),out number))            {                if (rating >= number)                {                    return OnBrush;                }                return OffBrush;            }            return Brushes.transparent;        }        public object ConvertBack(object value,System.Globalization.CultureInfo culture)        {            throw new NotImplementedException();        }    }}

用法:

<Window x:Class="WpfApplication14.MainWindow"        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        xmlns:l="clr-namespace:WpfApplication14"         title="MainWindow" Height="350" WIDth="525">    <GrID>        <l:UserControl1 ratingValue="3" />    </GrID></Window>

结果:

总结

以上是内存溢出为你收集整理的C#WPF等级控制类似于Wifi信号指示器全部内容,希望文章能够帮你解决C#WPF等级控制类似于Wifi信号指示器所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1218635.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存