Note that there are some explanatory texts on larger screens.

plurals
  1. POAttach event handlers to controls within a data template
    primarykey
    data
    text
    <p>I have a list box in WPF which displays the status of a number of worker threads. Included in the display is a progress bar.</p> <p>It turns out that progress bars have an issue (defect?): the "Maximum" value is a dependency property, but the progress bar does not update when this property changes.</p> <p>Somebody encounted a similar problem in <a href="https://stackoverflow.com/questions/655780/progressbar-not-updating-on-change-to-maximum-through-binding">this question</a>. The suggested solution is to add a handler to the progress bar that deals with the dependency property changing.</p> <p>But my progress bar is created by the WPF framework through a data template. (See code below) How could I attach a handler to these dynamically created progress bars?</p> <p>Or, is there another way to solve this problem?</p> <pre><code> &lt;DataTemplate x:Key="DataTemplateTransferWorker"&gt; &lt;Border Style="{StaticResource TransferWorker}" Height="Auto"&gt; &lt;StackPanel Orientation="Vertical"&gt; &lt;TextBlock Style="{StaticResource ItemHeader}"&gt;Transfer Worker&lt;/TextBlock&gt; &lt;TextBlock Style="{StaticResource FieldValue}" Text="{Binding StatusDescription}" /&gt; &lt;!-- Note that the maximum comes from a changing property. --&gt; &lt;ProgressBar Margin="6, 3, 6, 3" Height="12" Orientation="Horizontal" Background="Transparent" Maximum="{Binding ServerJob.FileSize, Mode=OneWay}" Value="{Binding BytesUploaded, Mode=OneWay}" /&gt; </code></pre> <p><hr /> <strong>Later Edit</strong></p> <p>It turns out that it's not as simple as the Maximum binding value being broken, because there are working examples where progress bars dynamically updates perfectly well.</p> <p>It wasn't an INotifyPropertyChanged issue. I was notifying of any changes to the ServerJob property, (but not the ServerJob.FileSize property which is constant for each server job.) I also put a breakpoint on ServerJob.FileSize.get, and the framework was indeed querying the filesize in response to the property changed notification.</p> <p>I also added two TextBlocks to the control, bound to ServerJob.FileSize and BytesUploaded respectively. And they updated exactly as they should.</p> <p>But for some reason, as soon as the BytesUploaded value became non-zero the progress bar would zoom to 100%. </p> <p>I've worked around the problem by adding a new property "PercentageComplete" which calculates the progress, and that way I can keep the maximum at a constant value of 100.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload