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

C#WPF等级控制类似于Wifi信号指示器,c#,wpf-controls,C#,Wpf Controls,我已经搜索了很多WPF等级控制simliar到下图中的wifi信号强度指示器,但我找不到 我试着自己做,结果是:) 我需要根据评级值更改矩形颜色,例如: <l:UserControl1 RatingValue="3" /> 这将为前三个矩形着色 有人能帮我做这件事吗,或者找一个类似的控件吗?您可以创建一个IValueConverter来更改矩形的颜色 下面是一个非常快速(粗略)的示例: Xaml: <Grid Background="DarkBlue

我已经搜索了很多WPF等级控制simliar到下图中的wifi信号强度指示器,但我找不到

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


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

        <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,0,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, Converter={StaticResource RatingConverter}, ConverterParameter=2}"/>
        <Rectangle Width="5" Height="15" Fill="{Binding RatingValue, Converter={StaticResource RatingConverter}, ConverterParameter=3}"/>
        <Rectangle Width="5" Height="20" Fill="{Binding RatingValue, Converter={StaticResource RatingConverter}, ConverterParameter=4}"/>
        <Rectangle Width="5" Height="25" Fill="{Binding RatingValue, Converter={StaticResource RatingConverter}, 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, Type targetType, object parameter, 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>
<Grid Background="Transparent" DataContext="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=loc:Wifi}}"
    d:DataContext="{d:DesignInstance Type=loc:Wifi, IsDesignTimeCreatable=True}">
    <Grid.Resources>
        <loc:QualityRateConverter x:Key="QualityRateConverter" OnBrush="#FF3DEA22" OffBrush="#99707070" />
    </Grid.Resources>
    <Path Stroke="{Binding Path=QualityRateValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource QualityRateConverter}, ConverterParameter=95}"
          StrokeThickness="2" Data="M23,5 A60,200 0 0 0 2,5"/>
    <Path Stroke="{Binding Path=QualityRateValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource QualityRateConverter}, ConverterParameter=75}"
          StrokeThickness="2" Data="M20,8 A55,200 0 0 0 4.5,8"/>
    <Path Stroke="{Binding Path=QualityRateValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource QualityRateConverter}, ConverterParameter=40}"
          StrokeThickness="2" Data="M17,11 A45,200 0 0 0 7.5,11"/>
    <Path Stroke="{Binding Path=QualityRateValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource QualityRateConverter}, ConverterParameter=20}"
          StrokeThickness="1" Fill="{Binding Path=QualityRateValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource QualityRateConverter}, ConverterParameter=20}">
        <Path.Data>
            <GeometryGroup FillRule="EvenOdd">
                <EllipseGeometry RadiusX="1.5" RadiusY="1.5" Center="12,15" />
            </GeometryGroup>
        </Path.Data>
    </Path>
</Grid>
public partial class Wifi : UserControl
    {
        public Wifi()
        {
            InitializeComponent();
        }
        public int QualityRateValue
        {
            get { return (int)GetValue(QualityRateValueProperty); }
            set { SetValue(QualityRateValueProperty, value); }
        }

        // Using a DependencyProperty as the backing store for RatingValue.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty QualityRateValueProperty =
            DependencyProperty.Register("QualityRateValue", typeof(int), typeof(Wifi), new UIPropertyMetadata(0));
    }
    public class QualityRateConverter : 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, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
<Grid Background="DarkBlue">
    <Viewbox Width="50" Height="50">
        <local:Wifi QualityRateValue="80"/>
    </Viewbox>

</Grid>


代码:

<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,0,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, Converter={StaticResource RatingConverter}, ConverterParameter=2}"/>
        <Rectangle Width="5" Height="15" Fill="{Binding RatingValue, Converter={StaticResource RatingConverter}, ConverterParameter=3}"/>
        <Rectangle Width="5" Height="20" Fill="{Binding RatingValue, Converter={StaticResource RatingConverter}, ConverterParameter=4}"/>
        <Rectangle Width="5" Height="25" Fill="{Binding RatingValue, Converter={StaticResource RatingConverter}, 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, Type targetType, object parameter, 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>
<Grid Background="Transparent" DataContext="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=loc:Wifi}}"
    d:DataContext="{d:DesignInstance Type=loc:Wifi, IsDesignTimeCreatable=True}">
    <Grid.Resources>
        <loc:QualityRateConverter x:Key="QualityRateConverter" OnBrush="#FF3DEA22" OffBrush="#99707070" />
    </Grid.Resources>
    <Path Stroke="{Binding Path=QualityRateValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource QualityRateConverter}, ConverterParameter=95}"
          StrokeThickness="2" Data="M23,5 A60,200 0 0 0 2,5"/>
    <Path Stroke="{Binding Path=QualityRateValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource QualityRateConverter}, ConverterParameter=75}"
          StrokeThickness="2" Data="M20,8 A55,200 0 0 0 4.5,8"/>
    <Path Stroke="{Binding Path=QualityRateValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource QualityRateConverter}, ConverterParameter=40}"
          StrokeThickness="2" Data="M17,11 A45,200 0 0 0 7.5,11"/>
    <Path Stroke="{Binding Path=QualityRateValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource QualityRateConverter}, ConverterParameter=20}"
          StrokeThickness="1" Fill="{Binding Path=QualityRateValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource QualityRateConverter}, ConverterParameter=20}">
        <Path.Data>
            <GeometryGroup FillRule="EvenOdd">
                <EllipseGeometry RadiusX="1.5" RadiusY="1.5" Center="12,15" />
            </GeometryGroup>
        </Path.Data>
    </Path>
</Grid>
public partial class Wifi : UserControl
    {
        public Wifi()
        {
            InitializeComponent();
        }
        public int QualityRateValue
        {
            get { return (int)GetValue(QualityRateValueProperty); }
            set { SetValue(QualityRateValueProperty, value); }
        }

        // Using a DependencyProperty as the backing store for RatingValue.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty QualityRateValueProperty =
            DependencyProperty.Register("QualityRateValue", typeof(int), typeof(Wifi), new UIPropertyMetadata(0));
    }
    public class QualityRateConverter : 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, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
<Grid Background="DarkBlue">
    <Viewbox Width="50" Height="50">
        <local:Wifi QualityRateValue="80"/>
    </Viewbox>

</Grid>
名称空间WpfApplication14
{
/// 
///UserControl1.xaml的交互逻辑
/// 
公共部分类UserControl1:UserControl
{
公共用户控制1()
{
初始化组件();
DataContext=this;
}
公共评级价值
{
获取{return(int)GetValue(RatingValueProperty);}
set{SetValue(RatingValueProperty,value);}
}
//使用DependencyProperty作为RatingValue的后备存储。这将启用动画、样式、绑定等。。。
公共静态只读从属属性RatingValueProperty=
DependencyProperty.Register(“RatingValue”、typeof(int)、typeof(UserControl1)、新UIPropertyMetadata(0));
}
公共类比率转换器:IValueConverter
{
公共笔刷OnBrush{get;set;}
公共刷刷{get;set;}
公共对象转换(对象值、类型targetType、对象参数、System.Globalization.CultureInfo区域性)
{
内部评级=0;
整数=0;
if(int.TryParse(value.ToString(),out rating)和&int.TryParse(parameter.ToString(),out number))
{
如果(额定值>=数量)
{
回刷;
}
回刷;
}
返回刷。透明;
}
公共对象转换回(对象值、类型targetType、对象参数、System.Globalization.CultureInfo区域性)
{
抛出新的NotImplementedException();
}
}
}
用法:

<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,0,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, Converter={StaticResource RatingConverter}, ConverterParameter=2}"/>
        <Rectangle Width="5" Height="15" Fill="{Binding RatingValue, Converter={StaticResource RatingConverter}, ConverterParameter=3}"/>
        <Rectangle Width="5" Height="20" Fill="{Binding RatingValue, Converter={StaticResource RatingConverter}, ConverterParameter=4}"/>
        <Rectangle Width="5" Height="25" Fill="{Binding RatingValue, Converter={StaticResource RatingConverter}, 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, Type targetType, object parameter, 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>
<Grid Background="Transparent" DataContext="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=loc:Wifi}}"
    d:DataContext="{d:DesignInstance Type=loc:Wifi, IsDesignTimeCreatable=True}">
    <Grid.Resources>
        <loc:QualityRateConverter x:Key="QualityRateConverter" OnBrush="#FF3DEA22" OffBrush="#99707070" />
    </Grid.Resources>
    <Path Stroke="{Binding Path=QualityRateValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource QualityRateConverter}, ConverterParameter=95}"
          StrokeThickness="2" Data="M23,5 A60,200 0 0 0 2,5"/>
    <Path Stroke="{Binding Path=QualityRateValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource QualityRateConverter}, ConverterParameter=75}"
          StrokeThickness="2" Data="M20,8 A55,200 0 0 0 4.5,8"/>
    <Path Stroke="{Binding Path=QualityRateValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource QualityRateConverter}, ConverterParameter=40}"
          StrokeThickness="2" Data="M17,11 A45,200 0 0 0 7.5,11"/>
    <Path Stroke="{Binding Path=QualityRateValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource QualityRateConverter}, ConverterParameter=20}"
          StrokeThickness="1" Fill="{Binding Path=QualityRateValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource QualityRateConverter}, ConverterParameter=20}">
        <Path.Data>
            <GeometryGroup FillRule="EvenOdd">
                <EllipseGeometry RadiusX="1.5" RadiusY="1.5" Center="12,15" />
            </GeometryGroup>
        </Path.Data>
    </Path>
</Grid>
public partial class Wifi : UserControl
    {
        public Wifi()
        {
            InitializeComponent();
        }
        public int QualityRateValue
        {
            get { return (int)GetValue(QualityRateValueProperty); }
            set { SetValue(QualityRateValueProperty, value); }
        }

        // Using a DependencyProperty as the backing store for RatingValue.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty QualityRateValueProperty =
            DependencyProperty.Register("QualityRateValue", typeof(int), typeof(Wifi), new UIPropertyMetadata(0));
    }
    public class QualityRateConverter : 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, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
<Grid Background="DarkBlue">
    <Viewbox Width="50" Height="50">
        <local:Wifi QualityRateValue="80"/>
    </Viewbox>

</Grid>

结果:

<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,0,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, Converter={StaticResource RatingConverter}, ConverterParameter=2}"/>
        <Rectangle Width="5" Height="15" Fill="{Binding RatingValue, Converter={StaticResource RatingConverter}, ConverterParameter=3}"/>
        <Rectangle Width="5" Height="20" Fill="{Binding RatingValue, Converter={StaticResource RatingConverter}, ConverterParameter=4}"/>
        <Rectangle Width="5" Height="25" Fill="{Binding RatingValue, Converter={StaticResource RatingConverter}, 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, Type targetType, object parameter, 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>
<Grid Background="Transparent" DataContext="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=loc:Wifi}}"
    d:DataContext="{d:DesignInstance Type=loc:Wifi, IsDesignTimeCreatable=True}">
    <Grid.Resources>
        <loc:QualityRateConverter x:Key="QualityRateConverter" OnBrush="#FF3DEA22" OffBrush="#99707070" />
    </Grid.Resources>
    <Path Stroke="{Binding Path=QualityRateValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource QualityRateConverter}, ConverterParameter=95}"
          StrokeThickness="2" Data="M23,5 A60,200 0 0 0 2,5"/>
    <Path Stroke="{Binding Path=QualityRateValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource QualityRateConverter}, ConverterParameter=75}"
          StrokeThickness="2" Data="M20,8 A55,200 0 0 0 4.5,8"/>
    <Path Stroke="{Binding Path=QualityRateValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource QualityRateConverter}, ConverterParameter=40}"
          StrokeThickness="2" Data="M17,11 A45,200 0 0 0 7.5,11"/>
    <Path Stroke="{Binding Path=QualityRateValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource QualityRateConverter}, ConverterParameter=20}"
          StrokeThickness="1" Fill="{Binding Path=QualityRateValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource QualityRateConverter}, ConverterParameter=20}">
        <Path.Data>
            <GeometryGroup FillRule="EvenOdd">
                <EllipseGeometry RadiusX="1.5" RadiusY="1.5" Center="12,15" />
            </GeometryGroup>
        </Path.Data>
    </Path>
</Grid>
public partial class Wifi : UserControl
    {
        public Wifi()
        {
            InitializeComponent();
        }
        public int QualityRateValue
        {
            get { return (int)GetValue(QualityRateValueProperty); }
            set { SetValue(QualityRateValueProperty, value); }
        }

        // Using a DependencyProperty as the backing store for RatingValue.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty QualityRateValueProperty =
            DependencyProperty.Register("QualityRateValue", typeof(int), typeof(Wifi), new UIPropertyMetadata(0));
    }
    public class QualityRateConverter : 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, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
<Grid Background="DarkBlue">
    <Viewbox Width="50" Height="50">
        <local:Wifi QualityRateValue="80"/>
    </Viewbox>

</Grid>

以下是显示Wifi信号质量指示器的另一种方式:

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,0,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, Converter={StaticResource RatingConverter}, ConverterParameter=2}"/>
        <Rectangle Width="5" Height="15" Fill="{Binding RatingValue, Converter={StaticResource RatingConverter}, ConverterParameter=3}"/>
        <Rectangle Width="5" Height="20" Fill="{Binding RatingValue, Converter={StaticResource RatingConverter}, ConverterParameter=4}"/>
        <Rectangle Width="5" Height="25" Fill="{Binding RatingValue, Converter={StaticResource RatingConverter}, 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, Type targetType, object parameter, 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>
<Grid Background="Transparent" DataContext="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=loc:Wifi}}"
    d:DataContext="{d:DesignInstance Type=loc:Wifi, IsDesignTimeCreatable=True}">
    <Grid.Resources>
        <loc:QualityRateConverter x:Key="QualityRateConverter" OnBrush="#FF3DEA22" OffBrush="#99707070" />
    </Grid.Resources>
    <Path Stroke="{Binding Path=QualityRateValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource QualityRateConverter}, ConverterParameter=95}"
          StrokeThickness="2" Data="M23,5 A60,200 0 0 0 2,5"/>
    <Path Stroke="{Binding Path=QualityRateValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource QualityRateConverter}, ConverterParameter=75}"
          StrokeThickness="2" Data="M20,8 A55,200 0 0 0 4.5,8"/>
    <Path Stroke="{Binding Path=QualityRateValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource QualityRateConverter}, ConverterParameter=40}"
          StrokeThickness="2" Data="M17,11 A45,200 0 0 0 7.5,11"/>
    <Path Stroke="{Binding Path=QualityRateValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource QualityRateConverter}, ConverterParameter=20}"
          StrokeThickness="1" Fill="{Binding Path=QualityRateValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource QualityRateConverter}, ConverterParameter=20}">
        <Path.Data>
            <GeometryGroup FillRule="EvenOdd">
                <EllipseGeometry RadiusX="1.5" RadiusY="1.5" Center="12,15" />
            </GeometryGroup>
        </Path.Data>
    </Path>
</Grid>
public partial class Wifi : UserControl
    {
        public Wifi()
        {
            InitializeComponent();
        }
        public int QualityRateValue
        {
            get { return (int)GetValue(QualityRateValueProperty); }
            set { SetValue(QualityRateValueProperty, value); }
        }

        // Using a DependencyProperty as the backing store for RatingValue.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty QualityRateValueProperty =
            DependencyProperty.Register("QualityRateValue", typeof(int), typeof(Wifi), new UIPropertyMetadata(0));
    }
    public class QualityRateConverter : 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, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
<Grid Background="DarkBlue">
    <Viewbox Width="50" Height="50">
        <local:Wifi QualityRateValue="80"/>
    </Viewbox>

</Grid>
用法:

<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,0,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, Converter={StaticResource RatingConverter}, ConverterParameter=2}"/>
        <Rectangle Width="5" Height="15" Fill="{Binding RatingValue, Converter={StaticResource RatingConverter}, ConverterParameter=3}"/>
        <Rectangle Width="5" Height="20" Fill="{Binding RatingValue, Converter={StaticResource RatingConverter}, ConverterParameter=4}"/>
        <Rectangle Width="5" Height="25" Fill="{Binding RatingValue, Converter={StaticResource RatingConverter}, 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, Type targetType, object parameter, 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>
<Grid Background="Transparent" DataContext="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=loc:Wifi}}"
    d:DataContext="{d:DesignInstance Type=loc:Wifi, IsDesignTimeCreatable=True}">
    <Grid.Resources>
        <loc:QualityRateConverter x:Key="QualityRateConverter" OnBrush="#FF3DEA22" OffBrush="#99707070" />
    </Grid.Resources>
    <Path Stroke="{Binding Path=QualityRateValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource QualityRateConverter}, ConverterParameter=95}"
          StrokeThickness="2" Data="M23,5 A60,200 0 0 0 2,5"/>
    <Path Stroke="{Binding Path=QualityRateValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource QualityRateConverter}, ConverterParameter=75}"
          StrokeThickness="2" Data="M20,8 A55,200 0 0 0 4.5,8"/>
    <Path Stroke="{Binding Path=QualityRateValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource QualityRateConverter}, ConverterParameter=40}"
          StrokeThickness="2" Data="M17,11 A45,200 0 0 0 7.5,11"/>
    <Path Stroke="{Binding Path=QualityRateValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource QualityRateConverter}, ConverterParameter=20}"
          StrokeThickness="1" Fill="{Binding Path=QualityRateValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource QualityRateConverter}, ConverterParameter=20}">
        <Path.Data>
            <GeometryGroup FillRule="EvenOdd">
                <EllipseGeometry RadiusX="1.5" RadiusY="1.5" Center="12,15" />
            </GeometryGroup>
        </Path.Data>
    </Path>
</Grid>
public partial class Wifi : UserControl
    {
        public Wifi()
        {
            InitializeComponent();
        }
        public int QualityRateValue
        {
            get { return (int)GetValue(QualityRateValueProperty); }
            set { SetValue(QualityRateValueProperty, value); }
        }

        // Using a DependencyProperty as the backing store for RatingValue.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty QualityRateValueProperty =
            DependencyProperty.Register("QualityRateValue", typeof(int), typeof(Wifi), new UIPropertyMetadata(0));
    }
    public class QualityRateConverter : 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, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
<Grid Background="DarkBlue">
    <Viewbox Width="50" Height="50">
        <local:Wifi QualityRateValue="80"/>
    </Viewbox>

</Grid>

结果

我一直想像比尔·盖茨那样发财,但我做不到,因为我不富有。。。有人能帮我致富吗?(更严肃地说……你尝试了什么?你的问题太广泛,没有具体的编码,所以,我想你不会得到太多帮助……)@Noctis无论如何,这是我试图满足陛下要求的代码。这他妈的怎么不具体编码?这是一个很好的答案。。。你认为如果没有你进一步的代码和解释,他会这么做吗?:)@Noctis,不管有没有他的代码示例,这对我来说都是一个简单的问题,也许你应该安定下来,学会享受生活的乐趣;)_安顿下来,向后靠着,啜饮着玛格丽塔,思考人们为什么会这样做_@诺蒂斯,在我工作的时候啜饮着玛格丽塔酒,好吧,现在我只是嫉妒:)相信我,我正在经历的代码会让你想要一杯玛格丽塔酒。。。回到代码矿,代码猴子。。。走开!!!:)