嗨我在wpf(Xaml)和Code-Behind中定义了一个自定义进度条,为了实现这个,我使用了INotifyPropertyChanege但是当它打开时没有在线工作:
enter code here
CustomizeProgressBar c=new CustomizeProgressBar();
c.Maximum=100;
c.Minimum=0;
for(int i=0;i<100;i++){
Thread.sleep(1000);
c.Value++;
}
enter code here
但是如果把它增加按钮点击事件中的值它的工作,但这不是无用的,如:
enter code here
public void Button_Click(obj sender,event arg e){
c.Value++;
}
<Window x:Class="Gevom.CustomizeProgressBar"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Gevom"
mc:Ignorable="d"
Title="CustomizeProgressBar" WindowStyle="None" AllowsTransparency="True"
UseLayoutRounding="True" Height="280" Width="650">
<Window.Resources>
<local:StringConverter x:Key="StringConverter" />
</Window.Resources>
<Grid Background="#f2f2f2">
<Grid.RowDefinitions>
<RowDefinition Height="25"/>
<RowDefinition Height="3*"/>
<RowDefinition Height="2*"/>
<RowDefinition Height="1*"/>
</Grid.RowDefinitions>
<Grid Background="Transparent" MouseLeftButtonDown="Grid_MouseLeftButtonDown" Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="3" >
<TextBlock Margin="8,0,0,0" HorizontalAlignment="Left" TextOptions.TextFormattingMode="Display" Foreground="White" TextOptions.TextRenderingMode="ClearType"
Text="Massege Box" FontFamily="Lao UI" FontSize="12" TextAlignment="Center" VerticalAlignment="Center" />
<Button HorizontalAlignment="Right" Name="btnClose" Style="{StaticResource btnCross}" Click="btnClose_Click"
BorderBrush=" #4f4f4f" />
</Grid>
<Grid Margin="20,0" Grid.RowSpan="2" Grid.Row="1">
<StackPanel Orientation="Horizontal" >
<TextBlock x:Name="Percenttxt" TextOptions.TextFormattingMode="Ideal" TextOptions.TextRenderingMode="ClearType"
Foreground="#ff263145" FontSize="171.56" FontFamily="Lao UI" Text="{Binding Value,Converter={StaticResource StringConverter},UpdateSourceTrigger=PropertyChanged}" />
<TextBlock TextOptions.TextFormattingMode="Ideal" TextOptions.TextRenderingMode="ClearType" Foreground="#ff263145" FontSize="94.30" VerticalAlignment="Center" FontFamily="Lao UI">%</TextBlock>
</StackPanel>
</Grid>
<Viewbox Grid.Row="2" Grid.RowSpan="2" Margin="20,0" >
<Grid>
<TextBlock Panel.ZIndex="2" Foreground="#ff7d7f7d" HorizontalAlignment="Right" Margin="0,10,70,0" FontSize="14.00" TextOptions.TextFormattingMode="Ideal" TextOptions.TextRenderingMode="ClearType" FontFamily="Myriad Pro">In Progress . . .</TextBlock>
<Path Fill="#ffe5e5e5" Data="F1 M 560.000,9.500 L 560.000,36.000 L 9.500,36.000 C 4.253,36.000 0.000,40.253 0.000,45.500 L 0.000,73.500 C 0.000,78.747 4.253,83.000 9.500,83.000 L 727.500,83.000 C 732.747,83.000 737.000,78.747 737.000,73.500 L 737.000,36.000 L 737.000,9.500 C 737.000,4.253 732.747,0.000 727.500,0.000 L 569.500,0.000 C 564.253,0.000 560.000,4.253 560.000,9.500 Z"/>
<Path StrokeThickness="0.3" Stroke="#ffb2b2b2" StrokeMiterLimit="1.0" Fill="#ffcbcbcb" Data="F1 M 718.000,63.000 L 20.000,63.000 L 20.000,57.000 L 718.000,57.000 L 718.000,63.000 Z"/>
<Line x:Name="ProgressLine" Stroke=" #cb7f21" StrokeThickness="6" Y1="60" X1="20" Y2="60" X2="{Binding MainValue,UpdateSourceTrigger=PropertyChanged}" />
</Grid>
</Viewbox>
</Grid>
</Window>
enter code here
代码隐藏
在这里输入代码
public partial class CustomizeProgressBar : Window,INotifyPropertyChanged
{
int _maximum;
int _minimum;
int _value;
int _mainValue;
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChange(string PropertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
}
}
public int Maximum
{
set
{
_maximum = value;
}
get
{
return _maximum;
}
}
public int Minimum
{
set
{
_minimum = value;
}
get
{
return _minimum;
}
}
public int MainValue
{
set
{
if (_mainValue != value)
{
_mainValue = value;
OnPropertyChange("MainValue");
Percenttxt.GetBindingExpression(TextBlock.TextProperty).UpdateTarget();
}
}
get
{
return _mainValue;
}
}
public int Value
{
set
{
if (value <= Maximum && value >= Minimum) {
if (_value !=value)
{
_value = value;
MainValue = 20 + ((718 ) / (Maximum - Minimum)) *_value;
OnPropertyChange("Value");
}
}
}
get
{
return _value;
}
}
public CustomizeProgressBar()
{
InitializeComponent();
MainValue = 20;
DataContext = this;
}
private void btnClose_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (e.ChangedButton == MouseButton.Left)
this.DragMove();
}
}
[ValueConversion(typeof(object), typeof(string))]
public class StringConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value == null ? null : value.ToString();
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
enter code here