ItemsControl在数据更改为ObservableCollection <t>属性后未更新

时间:2015-12-12 21:21:22

标签: c# xaml uwp

我有以下测试代码...... XAML:

<Page
    x:Class="Test.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Test"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:data="using:Test"
    mc:Ignorable="d">

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <Button Name="StartButton" Content="Start" Click="StartButton_Click" Height="30" Width="200"/>

        <ItemsControl Grid.Row="1" Background="Gray" ItemsSource="{x:Bind RowItems}">
        <ItemsControl.ItemTemplate>

                <DataTemplate x:DataType="data:MyData">
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{x:Bind FirstName}" Margin="10,0,5,0"/>
                        <TextBlock Text="{x:Bind Surname}"/>
                    </StackPanel>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </Grid>
</Page>

代码背后:

namespace Test
{
    public class MyData
    {
        public string FirstName { get; set; }
        public string Surname { get; set; }
    }

    public class LoadData
    {
        public static void Get_RowItems(ObservableCollection<MyData> Items)
        {
            Items.Add(new MyData() { FirstName = "John", Surname = "Doe" });
        }
    }

    public sealed partial class MainPage : Page
    {
        private ObservableCollection<MyData> RowItems;

        public MainPage()
        {
            this.InitializeComponent();
            RowItems = new ObservableCollection<MyData>();
            LoadData.Get_RowItems(RowItems);
        }

        private void StartButton_Click(object sender, RoutedEventArgs e)
        {
            RowItems[0].FirstName = "Bill";
        }
    }
}

在运行它时,ItemsControl显示第一行数据(John Doe)。

单击“开始”按钮时,基础数据将更改为Bill Doe(按预期方式)

但是,ItemsControl顽固地继续显示:John Doe

我读过很多关于这类问题的文章,似乎没有任何解决方案可行(如果我已正确实施的话),所以现在我看不到树木了。

任何帮助都将不胜感激。

3 个答案:

答案 0 :(得分:4)

就像@Clemens所说,你应该在INotifyPropertyChanged类中实现MyData事件,以便在类的属性发生变化时通知UI。

public class MyData:INotifyPropertyChanged
{
    //public string FirstName { get; set; }
    private string firstName;

    public string FirstName
    {
        get { return firstName; }
        set { firstName = value;
        OnPropertyChanged("FirstName");
        }
    }

    private string surName;

    public string Surname
    {
        get { return surName; }
        set { surName = value;
        OnPropertyChanged("Surname");

        }
    }

    //public string Surname { get; set; }

    public event PropertyChangedEventHandler PropertyChanged;
    public void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

您不是要修改ObservableCollection本身(例如添加/删除项目),而是修改项目 INSIDE ObservableCollection负责通知自己的更改,而不是与其项目相关的更改。您应该在setter课程的MyData中NotifyPropertyChange(“EveryYourProperty”),您的用户界面将会更新。

并将您的TextBlock和“姓氏”属性的绑定更改为“OneWay”,因为默认设置为“OneTime”:

<TextBlock Text="{x:Bind FirstName, Mode=OneWay}" Margin="10,0,5,0"/>
<TextBlock Text="{x:Bind Surname, Mode=OneWay}"/>

As MSDN says

单向 - 在绑定源(源)更改时更新绑定目标(目标)属性。如果绑定的控件是隐式只读的,则这种类型的绑定是合适的。

一次性 - 在应用程序启动或数据上下文更改时更新绑定目标。如果您使用的数据适用于当前状态的快照或数据是真正静态的,则此类绑定是合适的。

更新1:

如果在更改ObservableCollection<T>的项目后执行耗时的操作,最好在非UI线程上执行耗时的操作:

 private void Button_Click(object sender, RoutedEventArgs e)
    {
        coll[0].FirstName = "Bill";
        Task.Run(()=> {
               Thread.Sleep(5000);
               MessageBox.Show("");
                       });
    }  

答案 1 :(得分:3)

克莱门斯是对的。此外,x:Bind的默认值是OneTime绑定,因此它不会得到更新。相反,你需要像:

<TextBlock Text="{x:Bind FirstName, Mode=OneWay}" Margin="10,0,5,0"/>

和其他地方一样,你正在使用x:Bind。

答案 2 :(得分:2)

根据提供的答案,您可以更进一步,为您的实体定义一个公共类,并以强类型方式通知属性值已更改的视图:

public abstract class BaseEntity : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    /// <summary>
    /// Raises the PropertyChanged event
    /// </summary>
    /// <param name="propertyName">Name of the property</param>
    public void OnPropertyChanged(string propertyName)
    {
        var ev = PropertyChanged;
        if (ev != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

    /// <summary>
    /// Raises the PropertyChanged event
    /// </summary>
    /// <param name="expr">Lambda expression that identifies the updated property</param>
    public void OnPropertyChanged<TProp>(Expression<Func<BaseEntity, TProp>> expr) 
    {
        var prop = (MemberExpression)expr.Body;
        OnPropertyChanged(prop.Member.Name);
    }
}

public class MyData : BaseEntity 
{
    private string firstName;

    public string FirstName
    {
        get { return firstName; }
        set { firstName = value; OnPropertyChanged(e => FirstName); }
    }

    private string surName;
    public string Surname
    {
        get { return surName; }
        set { surName = value; OnPropertyChanged(e => Surname); }
    }
 }