ItemsControl not updating immediately after Property Value change

蓝咒 提交于 2019-12-12 17:43:15

问题


I have the following test code…

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 Background="White">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

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

        <ItemsControl Name="MyItemsControl" Grid.Row="1"  Background="Gray" ItemsSource="{x:Bind RowItems, Mode=OneWay}">
        <ItemsControl.ItemTemplate>

                <DataTemplate x:DataType="data:MyData">
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{x:Bind FirstName, Mode=OneWay}" Margin="10,0,5,0"/>
                        <TextBlock Text="{x:Bind Surname, Mode=OneWay}"/>
                    </StackPanel>
                </DataTemplate>

            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </Grid>
</Page>

Code behind:

namespace Test
{
    public class MyData : INotifyPropertyChanged
    {
        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 event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }

    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";
            RowItems[0].Surname = "Smith";
        }

    }
}

While running this seems fine, the ItemsControl updates only after the StartButton_Click event handler has completed but I need it to update as each properties value changes inside the StartButton_Click event handler.

Before Start Button is click the display shows 'John Doe'

As the RowItems[0].FirstName = "Bill"; completes the display should show 'Bill Doe'

After the RowItems[0].Surname = "Smith"; completes the display should show 'Bill Smith'

I.E I need the display to change immediately after the property value changes.

Any help would be appreciated.


回答1:


Like I said above, your code looks right. The display renders asynchronously, so it may be just that the updates are getting batched together. Can you try something like:

    private async void StartButton_Click(object sender, RoutedEventArgs e)
    {
        RowItems[0].FirstName = "Bill";
        await Task.Delay(2000);
        RowItems[0].Surname = "Smith";
    }

This will pause for 2 seconds after setting the first name. I suspect you'll see "Bill Doe" for 2 seconds, then "Bill Smith".

If this is the case then everything is basically working as intended. In that case you'll need to explain more about what you want to happen. If the display showed "Bill Doe" for a fraction of a second and then showed "Bill Smith", what if it's updating faster than you can see?



来源:https://stackoverflow.com/questions/34256797/itemscontrol-not-updating-immediately-after-property-value-change

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!