简体   繁体   English

WinUI中的ListView在重置后维护ListBox文本

[英]ListView in WinUI maintains ListBox text after reset

I have a ListView in a WinUI 3 app (target framework net6.0-windows10.0.19041.0) with a data template that contains a TextBox.我在 WinUI 3 应用程序(目标框架 net6.0-windows10.0.19041.0)中有一个 ListView,其数据模板包含一个 TextBox。 The ItemsSource is bound to an ObservableCollection in the code-behind file. ItemsSource 绑定到代码隐藏文件中的 ObservableCollection。 There is also a button after the list to clear the collection and add a new item.列表后还有一个按钮,用于清除集合并添加新项目。

I noticed a strange behaviour: if i write anything in the TextBox, that text is retained after i click the Clear button.我注意到一个奇怪的行为:如果我在 TextBox 中写了任何内容,那么在我单击“清除”按钮后,该文本将被保留。 I would expect it to insert a completely new TextBox in the ListView, with no text in it.我希望它在 ListView 中插入一个全新的 TextBox,其中没有文本。

Why does this happen?为什么会这样? Does it have something to do with virtualization?它与虚拟化有关吗?

Here is the xaml file:这是 xaml 文件:

<Window
    x:Class="WinUISample.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:WinUISample"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
        <ListView ItemsSource="{x:Bind _items }" >
            <ListView.ItemTemplate>
                <DataTemplate x:DataType="x:String">
                    <TextBox Width="250" /> <!-- intentionally empty -->
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

        <Button Click="myButton_Click">Clear</Button>
    </StackPanel>
</Window>

And this is the code-behind file:这是代码隐藏文件:

using Microsoft.UI.Xaml;
using System.Collections.ObjectModel;

namespace WinUISample
{
    public sealed partial class MainWindow : Window
    {
        private readonly ObservableCollection<string> _items;

        public MainWindow()
        {
            InitializeComponent();

            _items = new ObservableCollection<string>(new[]
            {
                "1"
            });
        }

        private void myButton_Click(object sender, RoutedEventArgs e)
        {
            _items.Clear();
            _items.Add("1");
        }
    }
}

This should be because of its Virtualization feature.这应该是因为它的虚拟化功能。 Since you are not binding your Text , you'll get the same text.由于您没有绑定Text ,因此您将获得相同的文本。

You can avoid this behavior by binding your Text ,您可以通过绑定Text来避免这种行为,

<TextBox Width="250" Text="{x:Bind}" />

or by changing the ItemsPanel .或通过更改ItemsPanel

<ListView ItemsSource="{x:Bind _items}" >
    <!--<ListView.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel/>
        </ItemsPanelTemplate>
    </ListView.ItemsPanel>-->
    <ListView.ItemTemplate>
        <DataTemplate x:DataType="x:String">
            <TextBox Width="250" />
            <!-- intentionally empty -->
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM