ItemsControl 在属性值更改后不会立即更新 [英] ItemsControl not updating immediately after Property Value change

查看:27
本文介绍了ItemsControl 在属性值更改后不会立即更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下测试代码...

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>

背后的代码:

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

    }
}

虽然运行这看起来不错,但 ItemsControl 仅在 StartButton_Click 事件处理程序完成后更新,但我需要它随着 StartButton_Click 事件处理程序内的每个属性值的变化而更新.

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.

在点击开始按钮之前,显示屏显示John Doe"

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

作为 RowItems[0].FirstName = "Bill";完成显示应显示Bill Doe"

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

在 RowItems[0].Surname = "Smith" 之后;完成显示应显示比尔史密斯"

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.

任何帮助将不胜感激.

推荐答案

就像我上面说的,你的代码看起来是正确的.显示以异步方式呈现,因此可能只是更新被批量处理.你可以试试这样的:

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

这将在设置名字后暂停 2 秒钟.我怀疑你会看到Bill Doe"2 秒钟,然后是Bill 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".

如果是这种情况,那么一切基本上都按预期工作.在这种情况下,您需要更多地解释您想要发生的事情.如果显示屏显示Bill Doe"几分之一秒,然后显示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?

这篇关于ItemsControl 在属性值更改后不会立即更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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