c# - 如果集合值相同,为什么 LongListSelector 会添加额外的项目?

标签 c# xaml windows-phone-8

我昨天在为 WP 编写演示应用程序时注意到了这种奇怪的行为。通常我从不传递普通元素,也从不传递“相同”的东西——但这次我传递了。 当绑定(bind)到 int 或 string 类型的 observablecollection 时,如果值相同,控件将首先添加一项,然后添加两项,然后添加三项,然后添加四项。而不是每次都加一个。基本上它是这样做的:

Items.Count++

但是,如果我将项目(例如字符串)更改为唯一的东西(例如 GUID.ToString),它就会具有预期的行为。

这与 DataContext 的设置方式无关,也与我是否将 LayoutMode="List"IsGroupingEnabled="False"添加到控件无关。

为什么要这样做?这是预期的行为吗?

我花了几个小时寻找答案,所以请不要随意粘贴有关控件的随机链接:)

View 代码:

<phone:PhoneApplicationPage
x:Class="Bug.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True"
DataContext="{Binding RelativeSource={RelativeSource Self}}">

<StackPanel>
    <Button Click="Button_Click">Add</Button>
    <phone:LongListSelector  Width="300" Height="600" ItemsSource="{Binding Items}"/>
</StackPanel>
</phone:PhoneApplicationPage>

代码隐藏:

using System.Collections.ObjectModel;
using System.Windows;


namespace Bug
{
    public partial class MainPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();

            Items = new ObservableCollection<string>();

            //DataContext = this;
        }

        public ObservableCollection<string> Items { get; set; }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Items.Add("A");

            // This works as expected
            //Items.Add(Guid.NewGuid().ToString());
        }
    }

}

最佳答案

这是 LongListSelector 中的错误。它与 Guid 一起工作的原因是因为这将进行引用比较并避免错误。

以下是使用引用对象的解决方法:

public partial class MainPage : PhoneApplicationPage
{
    // Constructor
    public MainPage()
    {
        InitializeComponent();

        // Sample code to localize the ApplicationBar
        Items = new ObservableCollection<Object>();

    }
    public ObservableCollection<Object> Items { get; set; }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        Items.Add(new Object() { Value = "A" });

        // This works as expected
        //Items.Add(Guid.NewGuid().ToString());
    }

}
public class Object
{
    public string Value { get; set; }

    public override string ToString()
    {
        return Value;
    }
}

关于c# - 如果集合值相同,为什么 LongListSelector 会添加额外的项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22634409/

相关文章:

c# - WPF UserControl 的服务器端渲染

c# - Silverlight 文本修剪和换行问题

windows-phone-8 - 在 Windows Phone 8.1 应用程序模板中引用 Microsoft.Phone 时,在模块 mscorlib.dll 中找不到类型 System.SystemException

c# 不能声明具有相同参数的静态和非静态方法?

c# - 如何连接 C# 与 AUTOCAD 2010

wpf - 右侧的扩展按钮 : how to do it?

xaml - 创建圆形图像 Xaml

excel - 将 CSV 转换为 Excel Windows Phone 8

c# - DataTable load() 约束错误

C# Marshalling double* 从 C++ DLL?