问题
I'm using this code in a XAML page:
<TextBox ItemsSource="{Binding Posters, Converter={StaticResource collectionToFirstElementConverter}, Mode=TwoWay}" />
Posters is an ObsevableCollection and I'm using a converter where takes the collection and gets the first element of it.
As I'm using async procedures, where the textbox receives the object, this one has no elements (Count=0), and calls the converter.
I'm trying to update the textbox everytime the property add new elements, but not calls the converter.
I remember that in Silverlight or WPF, exists SourceTrigger or UpdatePropertyChanged, but in WinRT I can't see this mode.
回答1:
The easiest way to achieve that would be to modify your view model containing the Posters
property accordingly. I can see two ways to go about it (both asuming that your view model implements INotifyPropertyChanged
):
- Add an event handler to
Posters.CollectionChanged
and inside it raiseINotifyPropertyChanged.PropertyChanged
forPosters
. - Add another property
FirstPoster
returning the value of the first element inPosters
. In the view model add an event handler toPosters.CollectionChanged
and inside it raiseINotifyPropertyChanged.PropertyChanged
forFirstPoster
. This way you don't even need the converter.
I personally like the second approach better.
来源:https://stackoverflow.com/questions/11111432/how-to-refresh-the-ui-in-a-metro-app