Can't Update WPF ProgressBar (not the standard tight-loop issue) - c#

I am trying to use the ProgressBar as a representation of a percent value (I have not found an alternative control that looks right).
I am trying to set the value. It is my understanding that just setting the Value property should work, and update the control when my current event handler ends and control passes back to the UI.But no matter what I set the value to, the bar stays empty.
Here is my XAML:
<ProgressBar Name="LevelProgress" Maximum="100" Minimum="0" />
and my C#:
LevelProgress.Value = 43.0;
I have also tried:
LevelProgress.SetValue(ProgressBar.ValueProperty,43.0);
Even setting the Value property in the XAML definition does not work.
I really don't want to have to setup some big background thread thing just to set this value. Can anyone recommend a solution, or an alternative control?

Works for me
Do you
InitializeComponent();
Before?
LevelProgress.Value = 43.0;
I put it in the Loaded event and it worked there.
And I tried
<ProgressBar Name="LevelProgress" Maximum="100" Minimum="0" Value="43" />
And it works there. Something is wrong with the progress bar.

Related

How to do line break on MaterialDesignInXaml Snackbar?

Im trying to use a Snackbar to display information, but the text i want to display is too long to fit inside the small window horizontally, so the text isn't shown completely:
An automatic line break would be super ok.
Setting it manually wouldn't work, because it needs to display different messages.
I actually got it working by setting the property FontFamily:
FontFamily="{DynamicResource MaterialDesignFont}"
To make it working for arbitrary number of lines, just delete MaxHeight setter for ContentPresenter in SnackbarMessage Style:
MaxHeight="{Binding RelativeSource={RelativeSource AncestorType={x:Type wpf:Snackbar}}, Path=(wpf:SnackbarMessage.ContentMaxHeight)}"

WPF Binding on startup

In my WPF project, I have the Visibility of a Grid bound to a bool with a BooleanToVisibilityConverter:
<Grid ... Visibility="{Binding SelectedElement.HasError, Converter={StaticResource ResourceKey=BoolToVis}}">
However on startup the grid is always shown for a few milliseconds, even though the element/class holding the bool field is not even created, and after creation is defaulted to false.
How can I prevent the grid from showing up on startup? I tried to implement a FallbackValue for the binding, because the object in the path is not yet available, but setting "Visible" or "Hidden" here doesn't change anything.
(Posted on behalf of the OP).
Works fine with FallbackValue=Collapsed.

My User Control inside a DataGrid keep reseting

I'm new to WPF, so maybe I'm doing something really stupid. But..
Also is my first question here.
I made a custom ProgressBar UserControl to use inside a DataGrid.
All look to work fine except that every time the DataGrid refresh (reordering a column) the UserControl resets, all properties and dependency properties change to their default values and the constructor is called again. Look like my control is destroyed and recreated on every DataGrid's refresh.
Anyway to avoid that behavior?
I need to keep the changes history.
edit:
<DataGridTemplateColumn Header="Progress" Width="*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<local:SegmentedProgressBar HorizontalAlignment="Stretch" SegmentedProgressCustomUpdate="{Binding SegmentedProgressCustomUpdate}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
It sounds as if the DataGrid is data binding again. That would cause the rows to potentially get recreated and, therefore, the user control to get reset. That said, if you are pulling the progress bar off of some external data, you could dynamically set all of the properties for the progress bar control in the row created event.

Handling different view model states in MVVM

I’m using the MVVM pattern for my metro app. On my main page, the user presses a button and the app gets the current location of the user.
The process of getting the user’s location is asynchronous and I want the UI to change, so the button will be disabled and an indeterminate progress bar will display until the co-ordinates are returned.
What is the best way to manage this according to MVVM? Having a custom visual state? I notice that there are ApplicationViewStates and CommonStates, is it possible to add your own custom ones?
How would you do this?
I think you're looking for something like:
viewModel:
private async void CommandExecution()
{
IsAwaitingResponse=true;
var response = await _myService.DoNetworkCall()
IsAwaitingResponse=false;
}
If you're using one of the microsoft templates you should have a BooleanToVisibilityConverter sitting in your "Common" folder. You use it like so:
<Page.Resources>
<BooleanToVisibilityConverter x:key="booleanToVisibilityConverter"/>
</Page.Resources>
"..."
<ProgressBar Visibility={Binding IsAwaitingResponse,Converter={StaticResource booleanToVisibilityConverter}}" IsIndeterminate="true"/>
My xaml experience is with WPF and Silverlight, but I hope this suggestion works for a Metro app. You can bind the button's IsEnabled property and the progress bar's visibility property to a boolean property (maybe call it IsCurrentLocationComplete) on your view-model. You'll change this property's value when the async method is complete and your view will be updated. Setting the Button's property is straight forward, but you'll need a value converter (IValueConverter) for the progress bar's visibility one.

Databinding progress bar Value and Maximum in Silverlight

I'm trying to implement a progress bar to monitor the downloading of content in a HttpWebRequest. I have never done databinding with XAML before, can anyone please give me tips on how I can set the Maximum value of the progress bar to the file size and the Value to the size of the content downloaded.
Thanks!
This XAML:
<ProgressBar Value="{Binding DownloadProgress, Mode=OneWay}" Minimum="0" Maximum="{Binding DownloadSize, Mode=OneWay}" IsHitTestVisible="False" />
should do it.
Beware of this SL bug however. Make sure Maximum binding appears before the Value binding in the XAML.

Categories