简体   繁体   English

更新ItemsControl中的绑定属性

[英]Update bound property in ItemsControl

Quick description of my setup: 我的设置的简要说明:

Disclaimer: The code is only to show what I want to do. 免责声明:该代码仅用于显示我想要做的事情。 The command binding for instance is done with event triggers etc. I'm pretty sure this wouldn't even build, but I didn't want to waste space. 例如,命令绑定是通过事件触发器等完成的。我很确定这甚至不会构建,但我不想浪费空间。

My View: 我的观点:

<ItemsControl ItemsSource="{Binding Items}">
   <ItemsControl.ItemTemplate>
       <DataTemplate>
           <StackPanel>
               <TextBlock Text="{Binding IsFavorite, Converter={StaticResource FavoriteConverter}" Tap="{Binding FavoriteCommand, CommandParameter={Binding}}"/>
               <TextBlock Text="{Binding Title}"/>
            </StackPanel>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

My ViewModel: 我的ViewModel:

public class ViewModel : ViewModelBase
{
    public IList<Item> Items { get; set; }
    public RelayCommand<Item> Favorite
    {
       get
       {
           return new RelayCommand<Item>(item =>
                {
                    item.IsFavorite = !item.IsFavorite;
                    RaisePropertyChanged(string.Empty);
                };
        }
     }
}

My Model: 我的型号:

public class Item
{
    public string Title { get; set; }
    public bool IsFavorite { get; set; }
}

* My Question: * * 我的问题: *

How can I get the IsFavorite-Property to update without implementing INotifyPropertyChanged? 如何在不实施INotifyPropertyChanged的情况下更新IsFavorite-Property? It's a model class, and I wouldn't like creating a ViewModel for the sole purpose of updating one property. 它是一个模型类,我不想创建一个ViewModel,仅用于更新一个属性。 I thought that calling PropertyChanged with an empty string would update everything, but alas it didn't. 我认为用一个空字符串调用PropertyChanged会更新所有内容,但唉它没有。

* My Solution: * * 我的解决方案: *

public class ItemViewModel : ViewModelBase
{
    public Item Model { get; private set; }
    public bool IsFavorite
    {
        get { return Model.IsFavorite; }
        set
        {
            Model.IsFavorite = value;
            RaisePropertyChanged("IsFavorite");
        }
    }
}

If you are binding to the value and expect it to be changing at runtime, then you should really implement INotifyPropertyChanged since WPF's binding system uses that interface to know when it needs to update. 如果你绑定了这个值并期望它在运行时发生变化,那么你应该真正实现INotifyPropertyChanged因为WPF的绑定系统使用该接口来知道它何时需要更新。

But with that said, you might be able to get away with raising a PropertyChange notification for the entire Items collection, although I'm not entirely sure that would work because the actual collection itself hasn't changed, only a property of one of the items inside the collection. 但话说回来,你可能会为整个Items集合提出一个PropertyChange通知,虽然我不完全确定它会起作用,因为实际的集合本身没有改变,只有一个属性的集合中的项目。 And WPF usually knows not to bother re-evaluating a property if it doesn't actually change. 如果没有实际改变,WPF通常知道不要重新评估一个属性。

RaisePropertyChanged("Items");

If it doesn't work, you could probably remove the item and re-add it to trigger a CollectionChange notification, however that also might not work and may cause other problems depending on your application design. 如果它不起作用,您可以删除该项并重新添加它以触发CollectionChange通知,但是这也可能不起作用,并且可能会导致其他问题,具体取决于您的应用程序设计。

// I may have the exact syntax wrong here
var index = Items.IndexOf(item);
var tmp = Items[index];
Items.Remove(tmp);
Items.Add(tmp, index);

But it would really just be best to implement INotifyPropertyChanged on your Item class. 但实际上,最好在Item类上实现INotifyPropertyChanged

Try to use: RaisePropertyChanged("Items"); 尝试使用: RaisePropertyChanged("Items"); and make Items collection ObservableCollection. 并使Items集合ObservableCollection。

Implementing INotifyPropertyChanged is better. 实现INotifyPropertyChanged更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM