Click here to Skip to main content
15,896,207 members
Please Sign up or sign in to vote.
2.00/5 (3 votes)
See more:
MainWindow.xaml
XML
<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.xaml.cs

C#
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

XML
<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>
Posted
Comments
johannesnestler 30-Apr-14 9:57am    
had to upvote your question cause someone voted with 1, but I think you have asked a "good question" showed the relevant code and respected the QA-Forum rules, so compared to others your question is valid and clear in my opinion (I hope my solution targets the real root of your problem)

1 solution

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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900