C# 虚拟化似乎正在破坏绑定的属性

C# 虚拟化似乎正在破坏绑定的属性,c#,data-binding,uwp,dependency-properties,virtualization,C#,Data Binding,Uwp,Dependency Properties,Virtualization,我有一个listview,作为一个datatemplate,它有一个相对复杂的UserControl,有几个绑定到它的属性 项目spanel如下所示: <ListView.ItemsPanel> <ItemsPanelTemplate> <ItemsStackPanel VerticalAlignment="Bottom" ItemsUpdatingScrollMode="KeepLastItemInView"/> &

我有一个listview,作为一个datatemplate,它有一个相对复杂的UserControl,有几个绑定到它的属性

项目spanel如下所示:

<ListView.ItemsPanel>
     <ItemsPanelTemplate>
          <ItemsStackPanel VerticalAlignment="Bottom" ItemsUpdatingScrollMode="KeepLastItemInView"/>
      </ItemsPanelTemplate>
</ListView.ItemsPanel>

我的listview通常包含大约70个项目,但我遇到了一个相当大的问题:

每次我向上滚动大约30个项目,然后向下滚动,属性似乎变得混乱,其中一些似乎与较高项目的属性发生了切换

例如,如果在我之前有以下情况:

<ListView.ItemsPanel>
     <ItemsPanelTemplate>
          <ItemsStackPanel VerticalAlignment="Bottom" ItemsUpdatingScrollMode="KeepLastItemInView"/>
      </ItemsPanelTemplate>
</ListView.ItemsPanel>
  • 属性A=1
  • 物业A=2
  • 物业A=3
上下滚动后,我会:

  • 属性A=1
  • 物业A=2
  • 属性A=1

如何确保项目保留其属性或正确重新加载它们?

我终于找到了解决方法:如果其他人遇到类似问题,可能是因为您在处理PropertyChanged事件时没有重置丢失或为空的属性。例如,如果这是listview的datatemplate中的usercontrol:

它是这样处理的:

    public string CustomText        
    {
        get { return (string)GetValue(CustomTextProperty); }
        set { SetValue(CustomTextProperty, value); }
    }
    public static readonly DependencyProperty CustomTextProperty = DependencyProperty.Register(
        nameof(CustomText),
        typeof(string),
        typeof(CustomControl),
        new PropertyMetadata(string.Empty, OnPropertyChanged));

    private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var instance = d as CustomControl;
        if(instance.CustomText != "") 
            myTextBlock.Text = instance.CustomText;
    }
每次虚拟化回收CustomControl时,如果它被绑定到一个空字符串,因为
if(instance.CustomText!=“”)
文本块将不会更新,旧属性将再次显示。如果OnPropertyChanged由于某种原因从未被调用,也会发生同样的情况


在这种特殊情况下,它似乎有点愚蠢,但在具有大量属性和子属性的大型用户控件上,它可能会变得相当混乱

我终于找到了解决方法:如果其他人遇到类似问题,这可能是因为您在处理PropertyChanged事件时没有重置丢失或为空的属性。例如,如果这是listview的datatemplate中的usercontrol:

它是这样处理的:

    public string CustomText        
    {
        get { return (string)GetValue(CustomTextProperty); }
        set { SetValue(CustomTextProperty, value); }
    }
    public static readonly DependencyProperty CustomTextProperty = DependencyProperty.Register(
        nameof(CustomText),
        typeof(string),
        typeof(CustomControl),
        new PropertyMetadata(string.Empty, OnPropertyChanged));

    private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var instance = d as CustomControl;
        if(instance.CustomText != "") 
            myTextBlock.Text = instance.CustomText;
    }
每次虚拟化回收CustomControl时,如果它被绑定到一个空字符串,因为
if(instance.CustomText!=“”)
文本块将不会更新,旧属性将再次显示。如果OnPropertyChanged由于某种原因从未被调用,也会发生同样的情况


在这种特殊情况下,这似乎有点愚蠢,但在具有大量属性和子属性的大型用户控件上,它可能会变得相当混乱

如果您阻止虚拟化,会产生什么结果?您是否在我们这边有一个项目要测试?我无法重现此问题。正如@SunteenWu MSFT所说的,你应该提供一个MCVE(你很可能会在处理问题时找出问题所在)。如果你阻止虚拟化,结果会怎样?你是否有一个项目需要我们测试?我无法重现这个问题。正如@SunteenWu MSFT所说的,你应该提供一个MCVE(你很可能会在处理问题时指出问题)。