繁体   English   中英

刷新ItemsControl WPF中的绑定

[英]refresh bindings in itemscontrol wpf

我有一个简单的项控件绑定到条目对象列表。 该按钮更新列表中每个项目的LastUpdated。 如何引发属性更改事件,以便在ItemsControl中更新LastUpdated字段。 我简化了我的示例,只是为了弄清绑定问题。 我的真实样本使用PRISM和第三方控件。

C#代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Input;

namespace TestItemsControl
{
    public class Entry
    {
        public string Name { get; set; }

        public DateTime LastUpdated { get; set; }
    }
}

namespace TestItemsControl
{
    public class TestViewModel: INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public List<Entry> Entries { get; set; }

        public ICommand UpdateCmd { get; set; }

        public TestViewModel()
        {
            this.Entries = new List<Entry>();
            this.Entries.Add(new Entry{ Name = "1", LastUpdated = DateTime.Now });
            this.Entries.Add(new Entry { Name = "2", LastUpdated = DateTime.Now });
            this.Entries.Add(new Entry { Name = "3", LastUpdated = DateTime.Now });
        }

        public void Refresh()
        {
            if (this.PropertyChanged!= null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("Entries"));
            }
        }
    }
}

XAML:

<Application x:Class="TestItemsControl.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:TestItemsControl"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <local:TestViewModel x:Key="viewModel"/>
    </Application.Resources>
</Application>


<Window x:Class="TestItemsControl.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:TestItemsControl"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525" DataContext="{StaticResource viewModel}">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <ItemsControl Grid.Row="0" ItemsSource="{Binding Entries}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Vertical"/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <TextBlock Text="{Binding Name}"/>
                        <TextBlock Text="{Binding LastUpdated}"/>
                    </Grid>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
        <Button Grid.Row="1" Content="Update" Click="Button_Click"/>
    </Grid>
</Window>

然后,您必须重写Entry

public class Entry: INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    public string Name { get; set; }

    DateTime lastUD;
    public DateTime LastUpdated
    {
        get
        {
            return lastUD;
        }
        set
        {
            lastUD = value;
            if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("LastUpdated"));
        }
    }
}

另外,将List<Entry>更改为ObservableCollection<Entry>

namespace TestItemsControl
{
    public class TestViewModel
    {
        public ObservableCollection<Entry> Entries=new ObservableCollection<Entry>();

        public ICommand UpdateCmd { get; set; }

        public TestViewModel()
        {
            this.Entries.Add(new Entry{ Name = "1", LastUpdated = DateTime.Now });
            this.Entries.Add(new Entry { Name = "2", LastUpdated = DateTime.Now });
            this.Entries.Add(new Entry { Name = "3", LastUpdated = DateTime.Now });
        }
    }
}

这样,您不必调用Refesh函数。

如果没有人订阅活动,则必须检查Null

public class Entry : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    public string Name { get; set; }

    DateTime lastUD;
    public DateTime LastUpdated
    {
        get
        {
            return lastUD;
        }
        set
        {
            lastUD = value;
            if(PropertyChanged != NULL)
               PropertyChanged(this, new PropertyChangedEventArgs("LastUpdated"));
        }
    }
}

我认为您必须在这里选择:

  1. 创建这样的方法:

     protected void OnPropertyChanged(string) { var handler = PropertyChanged; if (handler != null) { handler(this, new PropertyChangedEventArgs(propertyName)); } } 

然后,每当您要更新属性时,都可以:

OnPropertyChanged("TheNameOfTheProperty");

或者如果您想更新该类中的每个属性:

OnPropertyChanged(string.Empty);

如果不确定何时更新哪个属性,建议您选择后者。

  1. 您可以,但是我建议您不要这样:实例化一个新列表,添加更新的值,清除原始列表,将原始列表设置为新列表,然后更新列表的属性名称。

您没有绑定UpdateCmd并在任何地方调用Refresh,除非它在Click处理程序中,但是您应该使用MVVM方法来执行此操作。

将Click事件处理程序更改为来自xaml中的viewmodel的命令绑定,如下所示

<Button Grid.Row="1" Content="Update" Command={Binding UpdateCmd}/>

然后在视图模型的构造函数绑定中,创建一个新的RelayCommand或ICommand实现类的样子,以便构造器接收动作委托。

this.UpdateCmd  = new RelayCommand(this.Update);

另外,您可能应该将“刷新”更改为“更新”,以更新条目的时间戳。

public void Update()
{
    foreach (var entry in this.Entries)
    {
        entry.LastUpdated = DateTime.Now;
    }

    if (this.PropertyChanged!= null)
    {
        PropertyChanged(this, new PropertyChangedEventArgs("Entries"));
    }
}

但实际上,您也应该仅在Entry模型上实现INotifyPropertyChanged并在属性设置器上引发事件,绑定将被更新,而不会通知整个集合应被更新。

暂无
暂无

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

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