Настройка индикатора выполнения не работала с INotifyPropertyChange

привет, я определяю настраиваемую полосу выполнения в 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

codeBehind

введите код сюда

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

person amirhossein    schedule 09.05.2017    source источник
comment
Thread.sleep занимает поток пользовательского интерфейса, поэтому он не может обновляться. Используйте таймер, такой как таймер диспетчера, и вместо этого обновите в обратном вызове.   -  person sondergard    schedule 09.05.2017
comment
tanx .its Work. но если я снова использую его в программе, он снова работает? Это означает, что при использовании таймера диспетчера мы вызываем событие в dispatcherTimer.Tick, как это сделать, не вызывая событие? может быть?   -  person amirhossein    schedule 09.05.2017
comment
Дополнительные примеры см. В разделе stackoverflow. com / questions / 26285926 /, stackoverflow.com/questions/41025517/ и stackoverflow.com/questions/39318185/   -  person Peter Duniho    schedule 09.05.2017