【问题标题】:ItemsControl not updating immediately after Property Value change属性值更改后 ItemsControl 未立即更新
【发布时间】:2015-12-13 21:50:46
【问题描述】:

我有以下测试代码……

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 事件处理程序内的每个属性值都发生变化。

点击开始按钮前显示'John Doe'

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

在 RowItems[0].Surname = "Smith" 之后;完成显示应该显示“Bill Smith”

I.E 我需要在属性值更改后立即更改显示。

任何帮助将不胜感激。

【问题讨论】:

  • 这里的代码/XAML 看起来是正确的。您能告诉我们 StartButton_Click() 中的代码吗?
  • @gregstoll - 它应该在 Code Behind(向下滚动)窗格的底部。
  • 哈,对不起,我错过了!

标签: c# xaml uwp


【解决方案1】:

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

    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”。

如果是这种情况,那么一切基本上都按预期工作。在这种情况下,您需要更多地解释您想要发生的事情。如果显示屏在几分之一秒内显示“Bill Doe”,然后显示“Bill Smith”,如果它的更新速度比您看到的要快怎么办?

【讨论】:

  • 嗨@gregstoll。是的,它完成了这项工作。我在处理程序末尾使用断点对其进行测试,但什么也没发生。但是,添加 'async' 和你的 'await...' 并在之后放置一个断点,它确实会改变。非常感谢。
  • 哦,对于其他用户,这需要'using System.Threading.Tasks;'
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-06-15
  • 1970-01-01
  • 1970-01-01
  • 2023-04-03
  • 1970-01-01
  • 1970-01-01
  • 2018-05-23
相关资源
最近更新 更多