0

Hi I am trying to set up a progress bar with some text and a button to cancel, so I used a user control. I created properties to this user control (Min,Max,Value) to pass them to the internal progress bar. It works when I initialize my user control with these values but when I update the value the progress bar doesn't move. Does anyone have any idea why ? Of course, the work represented by the progress bar is done on a seperate thread and the values are indeed updated and goes from 0 to 100.

XAML

<Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="1*"/>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>
        <StackPanel Grid.Column="0">
            <ProgressBar x:Name="progress_bar_internal" Minimum="{Binding Min}" Maximum="{Binding Max}" Value="{Binding Value}" Foreground="DarkGreen" Height="{Binding Height}" VerticalAlignment="Center"/>
            <TextBlock Text="{Binding ElementName=progress_bar_internal, Path=Value, StringFormat={}{0:0}%}" HorizontalAlignment="Center" VerticalAlignment="Center" />
        </StackPanel>
        <wpfui:Button Icon="PaneClose24" Grid.Column="1" VerticalAlignment="Center" Height="{Binding Height}" x:Name="cancel_btn" Background="DarkRed"/>
    </Grid>

The code behind

public partial class ProgressBar_w_Text : UserControl
    {
        
        public ProgressBar_w_Text()
        {
            InitializeComponent();
            this.DataContext = this;
        }

        public event RoutedEventHandler Click
        {
            add { cancel_btn.AddHandler(ButtonBase.ClickEvent, value); }
            remove { cancel_btn.RemoveHandler(ButtonBase.ClickEvent, value); }
        }

        public int Value { get; set; }
        public int Max { get; set; }
        public int Min { get; set; }
    }

Call

<uc:ProgressBar_w_Text Visibility="Collapsed" x:Name="progress_bar" Height="50" Click="cancel_patcher" Min="0" Max="100" Value="15"/>
SamHuffman
  • 521
  • 1
  • 4
  • 10
  • 2
    The properties of your UserControl do not fire a change notification. You should declare them as dependency properties. Besides that, a UserControl should not explicitly set its own DataContext, because that would break the standard DataContext-based Bindings of its properties. Instead, bind to its properties like `Value="{Binding Value, RelativeSource={RelativeSource AncestorType=UserControl}}"`. – Clemens May 04 '22 at 12:05

1 Answers1

1

You should either implement the properties as dependency properties or implement the INotifyPropertyChanged interface and raise the PropertyChanged event whenever any property is set to a new value.

If you don't do any of this, the UI cannot be supposed to know when to refresh.

Here is an example of how to implement Value as a dependency property:

public static readonly DependencyProperty ValueProperty = DependencyProperty.Register(
    nameof(Value), typeof(int), typeof(ProgressBar_w_Text));

public int Value
{
    get => (int)GetValue(ValueProperty);
    set => SetValue(ValueProperty, value);
}

Dependency properties provide change notifications by default. Your current (CLR) properties do not.

mm8
  • 163,881
  • 10
  • 57
  • 88