我已经实现了“电池图标”以进度条的形式动态更新电池状态。但它没有反映在电池图标中。 [英] I have implemented "Battery icon" in the form of progress bar that updates battery status dynamically. But its not reflecting in battery icon.

查看:61
本文介绍了我已经实现了“电池图标”以进度条的形式动态更新电池状态。但它没有反映在电池图标中。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MainWindow.xaml

MainWindow.xaml

<Grid Width="50" Height="30" HorizontalAlignment="Left" Margin="0,0,-18,0">
                    <ProgressBar Style="{DynamicResource ProgressBar}" x:Name="BatteryStatus1" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" VerticalAlignment="Top"  SnapsToDevicePixels="True"  Background="Black" Margin="0" Height="20" Width="35" Value="{Binding BataryValue}">
                        <ProgressBar.ToolTip >
                            <ToolTip Placement="Bottom" Visibility="Visible">
                                <StackPanel>
                                    <!--<Label Content="50% remaining/ available (plugged in , charging)"/>-->
                                    <StackPanel Orientation="Vertical">
                                        <TextBlock Text="Power Availability : "/>
                                        <TextBlock Text="{Binding BataryValue}"/>
                                        <TextBlock Text="%"/>
                                    </StackPanel>
                                    <StackPanel Orientation="Vertical">
                                        <TextBlock Text="Remaining Time : "/>
                                        <TextBlock Text="{Binding BataryValue}"/>
                                        <TextBlock Text="min"/>
                                    </StackPanel>
                                </StackPanel>
                            </ToolTip>
                        </ProgressBar.ToolTip>
                    </ProgressBar>
                </Grid>









MainWindow .xam l.cs







MainWindow.xaml.cs

namespace WpfApplication3
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        public MainWindow()
        {
            InitializeComponent();

            this.DataContext = this;

            BataryValue = 100;

            System.Timers.Timer timer = new System.Timers.Timer(1000d);

            timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);

            timer.Start();
        }

        public double BataryValue { get; set; }

        void timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() => { this.BataryValue -= 1; NotifyPropertyChanged("BataryValue"); }));
        }


        private void NotifyPropertyChanged(String propertyName = "")
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

    }
}





ControlTemplate





ControlTemplate

<Style x:Key="ProgressBar" TargetType="ProgressBar">
           <Setter Property="Template">
               <Setter.Value>
                   <ControlTemplate TargetType="{x:Type ProgressBar}">
                       <Grid x:Name="BataryStatus" Height="20" Width="38" Background="Black">
                           <Rectangle Stroke="White" Margin="0,0,6,0" />
                           <Rectangle Fill="White" HorizontalAlignment="right" Margin="0,4"/>
                           <Rectangle Fill="White" Stroke="White" HorizontalAlignment="Right" Margin="0,4" Width="5" />
                           <Rectangle Fill="White" Stroke="White" HorizontalAlignment="Left" Margin="3,2,0,2" Width="5" />
                           <Rectangle Fill="White" Stroke="White" HorizontalAlignment="Left" Margin="10,2,0,2" Width="5" />
                           <Rectangle Fill="White" Stroke="White" HorizontalAlignment="Left" Margin="17,2,0,2" Width="5" />
                           <Rectangle Fill="White" Stroke="White" HorizontalAlignment="Right" Margin="0,2,16,2" Width="5" />
                           <Rectangle Fill="White" Stroke="White" HorizontalAlignment="Right" Margin="0,2,9,2" Width="5" />
                       </Grid>
                   </ControlTemplate>
               </Setter.Value>
           </Setter>
       </Style>

推荐答案

嗨会员,

快速浏览后,我想知道你在这里用INotifyPropertyChanged做什么?你应该在Property-Change(你的属性设置器)中调用它。无论如何,你的实现可能看起来不寻常但是它应该有用,如果你也告诉你的进度条你的初始值(100)并在它之后调用你的NotifyPropertyChanged。但最好将其更改为常用INotifyPropertyChanged模式。你的错误应该给你足够的理由。 (正常模式将始终通知观察者,如果您不希望通知发生,您可以操作(初始化)支持字段。



亲切的问候



Johannes
Hi Member,
After a quick look i was wondering what you do here with the INotifyPropertyChanged? you should call it on Property-Change (inside your property setter). Anyway, you implementation may look "unusual" but it should work, if you also inform your progressbar about your initial value (100) and call your NotifyPropertyChanged after it. But better change it to the "common" INotifyPropertyChanged pattern. Your mistake should give you reason enough to do so. (The normal pattern would always inform observers AND you can manipulate (initialize) the backing field if you don't want the notification to happen)

Kind regards

Johannes


这篇关于我已经实现了“电池图标”以进度条的形式动态更新电池状态。但它没有反映在电池图标中。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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