ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

电池 SimpleBattery

2021-12-13 18:35:14  阅读:175  来源: 互联网

标签:DependencyProperty double SimpleBattery public typeof 电池 Brush


xaml

<UserControl.Resources>
    <local:WithConverter x:Key="withConverter"/>
</UserControl.Resources>
<Grid x:Name="grid" Background="{Binding BackgroundStroke, Mode=TwoWay, RelativeSource={RelativeSource AncestorType=UserControl}}">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="14*"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <Grid Grid.Column="1">
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Border Grid.Row="2" Background="Transparent" BorderBrush="{Binding RimStroke, Mode=TwoWay, RelativeSource={RelativeSource AncestorType=UserControl}}" 
                BorderThickness="1"/>
    </Grid>
    <Border Grid.Column="0" Background="Transparent" BorderBrush="{Binding RimStroke, Mode=TwoWay, RelativeSource={RelativeSource AncestorType=UserControl}}" 
            BorderThickness="1">
        <Rectangle Margin="1" Fill="{Binding RangeStroke, Mode=TwoWay, RelativeSource={RelativeSource AncestorType=UserControl}}" HorizontalAlignment="Left">
            <Rectangle.Width>
                <MultiBinding Converter="{StaticResource withConverter}">
                    <Binding ElementName="grid"/>
                    <Binding Path="Maximum" RelativeSource="{RelativeSource AncestorType=UserControl}"/>
                    <Binding Path="ProgressValue" RelativeSource="{RelativeSource AncestorType=UserControl}"/>
                </MultiBinding>
            </Rectangle.Width>
        </Rectangle>
    </Border>
</Grid>

cs

/// <summary>
/// SimpleBattery.xaml 的交互逻辑
/// </summary>
public partial class SimpleBattery : UserControl
{
    public SimpleBattery()
    {
        InitializeComponent();
    }
    /// <summary>
    /// 描边颜色
    /// </summary>
    public Brush RimStroke
    {
        get => (Brush)GetValue(RimStrokeProperty);
        set => SetValue(RimStrokeProperty, value);
    }
    public static readonly DependencyProperty RimStrokeProperty =
        DependencyProperty.Register("RimStroke", typeof(Brush), typeof(SimpleBattery), new PropertyMetadata(default(Brush)));
    /// <summary>
    /// 背景颜色
    /// </summary>
    public Brush BackgroundStroke
    {
        get => (Brush)GetValue(BackgroundStrokeProperty);
        set => SetValue(BackgroundStrokeProperty, value);
    }
    public static readonly DependencyProperty BackgroundStrokeProperty =
        DependencyProperty.Register("BackgroundStroke", typeof(Brush), typeof(SimpleBattery), new PropertyMetadata(default(Brush)));
    /// <summary>
    /// 范围值颜色
    /// </summary>
    public Brush RangeStroke
    {
        get => (Brush)GetValue(RangeStrokeProperty);
        set => SetValue(RangeStrokeProperty, value);
    }
    public static readonly DependencyProperty RangeStrokeProperty =
        DependencyProperty.Register("RangeStroke", typeof(Brush), typeof(SimpleBattery), new PropertyMetadata(default(Brush)));

    /// <summary>
    /// 范围最大值
    /// </summary>
    public double Maximum
    {
        get => (double)GetValue(MaximumProperty);
        set => SetValue(MaximumProperty, value);
    }
    public static readonly DependencyProperty MaximumProperty =
        DependencyProperty.Register("Maximum", typeof(double), typeof(SimpleBattery), new PropertyMetadata(100d));


    /// <summary>
    /// 范围真值 
    /// </summary>
    public double ProgressValue
    {
        get => (double)GetValue(ProgressValueProperty);
        set => SetValue(ProgressValueProperty, value);
    }
    public static readonly DependencyProperty ProgressValueProperty =
        DependencyProperty.Register("ProgressValue", typeof(double), typeof(SimpleBattery), new PropertyMetadata(50d));


}

internal class WithConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        if (values.Length == 3 && values[0] is Grid grid && values[1] is double max && values[2] is double val)
        {
            var width = (grid.ColumnDefinitions[0].ActualWidth * val / max) - 2;
            return width;
        }
        return DependencyProperty.UnsetValue;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

demo

<wesson:SimpleBattery Width="100" Height="50"
                      BackgroundStroke="White"
                      RimStroke="Blue"
                      RangeStroke="Red"
                      ProgressValue="66"/>

prism


标签:DependencyProperty,double,SimpleBattery,public,typeof,电池,Brush
来源: https://www.cnblogs.com/wesson2019-blog/p/15684632.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有