本文介绍了我已经实现了“电池图标”以进度条的形式动态更新电池状态。但它没有反映在电池图标中。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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>
推荐答案
这篇关于我已经实现了“电池图标”以进度条的形式动态更新电池状态。但它没有反映在电池图标中。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!