如何刷新对象? [英] How can i Refresh a object?

查看:96
本文介绍了如何刷新对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个代表演员的财产

I have a property representing an actor

private Actor _actor;

public Actor Actor
{
    get => _actor;
    set
    {
        if (_actor != value) {
            _actor = value;
            OnPropertyChanged("Actor");
        }   
    }
}

和一个列表,带有根据Actor状态的复选标记.当我单击标签上的状态时,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;

但是我看不到我的ListView中的更改,为什么?

but I cannot see the changes in my ListView, why?

在我的列表中,我绑定了演员Text ="{Binding Actor.id}以发送我的转换器并更改项目检查

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}}"/>

推荐答案

据我所知,您正在使用标签文本来确定是否选择了演员.无论如何,您没有以任何方式绑定到该文本,但是您将Label本身用作绑定参数.我不确定,但是在我看来,绑定" 的值似乎不符合您的预期,因为更改通知未订阅这种方式.无论如何,当您刷新该列表时,将刷新绑定,并将转换器与新值一起使用.由于ConverterParameter不是可绑定的属性,因此我怀疑是否有任何机会以这种方式使用它.

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.

我要做的是扩展您的Actor类或使用该属性创建一个新的ActorViewModel

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));
    }
}

现在您可以从ListView绑定到该属性

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;
    }
}

这样,更改应反映在ListView中.

this way, the change should reflect in the ListView.

这篇关于如何刷新对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆