问题
I have a property representing an actor
private Actor _actor;
public Actor Actor
{
get => _actor;
set
{
if (_actor != value) {
_actor = value;
OnPropertyChanged("Actor");
}
}
}
and a list, with a checkmark that depends on the state of Actor
. When I click over the label the state of Actor
shall change the checkmark
private async void OnSelectionAsync(object item)
{
Actor = item;
but I cannot see the changes in my ListView
, why?
Edit 1:
in my list, i am binding the actor Text="{Binding Actor.id} to send my converter and to change the item check
<Label IsVisible="False" x:Name="dTd" Text="{Binding Actor.id}" />
<ListView ItemsSource="{Binding Actors}">
...
<Image Source="" IsVisible="{Binding id , Converter={StaticResource MyList}, ConverterParameter={x:Reference dTd}}"/>
回答1:
As far as I can tell, you are using the text of your label to determine whether an actor is selected or not. Anyway, you are not binding to that text in any way, but you are using the Label
itself as a binding parameter. I do not know for sure, but it seems to me as if "binding" the value this way does not work as intended by you, because change notifications are not subscribed to this way. Anyway, when you refresh that list, the bindings are refreshed and the converter is used with the new value. Since ConverterParameter
is not a bindable property I doubt that there is any chance to use it this way.
What I'd do is to either extend your Actor
class or create a new ActorViewModel
class with the property
public bool IsSelected
{
get => isSelected;
set
{
if (value == isSelected)
{
return;
}
isSelected = value;
OnPropertyChanged(nameof(IsSelected));
}
}
From your ListView
you can now bind to that property
<Image Source="..." IsVisible="{Binding IsSelected}"/>
All you have to do now is setting the property accordingly
private async void OnSelectionAsync(object item)
{
Actor = item as Actor;
foreach(var actor in Actors)
{
Actor.IsSelected = actor.id == Actor.id;
}
}
this way, the change should reflect in the ListView
.
来源:https://stackoverflow.com/questions/61199406/how-can-i-refresh-a-object