Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

How do I cause a rebinding of the Text property of a TextBlock when data changes elsewhere?

$
0
0

We've got a situation in which we've got several user controls we've defined, that collect data from the user. Some of the data is what we call "ratings". On one of the other user controls, which I'll call Summary, we've display all of the ratings that was saved in the other user controls. If the user progresses through the application in a sequential manner, then the by the time they get to Summary all of the ratings have been collected and saved, and Summary can display the ratings in several TextBlocks. All is good with the world.

But of course, users don't go through the app in a sequential manner. They'll go to some of the other user controls, enter data and ratings, jump ahead to Summary see that the ratings they've entered are there. But then they'll jump back to user controls they haven't been do, enter data and ratings into them, then jump back to Summary. Now they're confused, thinking that the ratings aren't being saved, but in fact they are. It's just that the Summary hasn't been disposed of. (I should have mentioned this earlier, so please forgive me for not mentioning it until now. The user controls, as the user visits them, are put into a dictionary where they've kept alive. This was a navigation pattern we decided to adopt for this application.)

I did a search on Stack Overflow to try and find some way of rebinding a TextBlock. This article WPF Force rebind looked promising, but it didn't work for me. So I'd like to have some guidance as to how I can handle this. We use CollectionViewSources to bind to, for example, here's one:

<CollectionViewSource x:Key="aSIDrugAlcoholViewSource" d:DesignSource="{d:DesignInstance {x:Type AsiEF:ASIDrugAlcohol}, CreateList=True}"/>

Now here's the XAML for the TextBlock that displays the rating:

<TextBlock Style="{StaticResource tblR}"
		   x:Name="tbAlcoholRating"
		   Text="{Binding RateNeedAlcoholTx, Source={StaticResource aSIDrugAlcoholViewSource}}"/>

Now here's how we fetch the data that's displayed into the aSIDrugAlcoholViewSource:

//declared at the class level
CollectionViewSource drugViewSource = null;

//Code later defined in the user control's Loaded event. remoteDB is an instance
//of an Entity Framework object that I wrote to fetch and save the data.
remoteDB.GetAsiDrugAlcohol(true, ClientNumber, CaseNumber);
drugViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("aSIDrugAlcoholViewSource")));
drugViewSource.Source = asiContext.ASIDrugAlcohols.Where(c => c.ClientNumber == ClientNumber && c.CaseNumber == CaseNumber);

So how do we refresh/rebind the Text property of the tbAlcoholRating TextBlock?


Rod


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>