listview中,我使用Wrappanel作为ItemsPanelTemplate

<ListView ItemsSource="{Binding TodoItemViewModels}"
    ScrollViewer.VerticalScrollBarVisibility="Disabled"
    Drop="Card_Drop"
    AllowDrop="True"
    DragLeave="Card_DragLeave"
    IsEnabled="{Binding Disable, ElementName=root}"
    Background="Green">

    <ListView.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel Margin="0" Orientation="Vertical" />
        </ItemsPanelTemplate>
    </ListView.ItemsPanel>


    <!-- Displaying Cards in CardCollection View -->
    <ListView.ItemTemplate>
        <DataTemplate>            
            <Border BorderThickness="2"
                BorderBrush="BlanchedAlmond" 
                Opacity="1"
                Background="BlanchedAlmond"
                Width="25" Height="30"
                MouseMove="Card_MouseMove"
                CornerRadius="5"
                HorizontalAlignment="Stretch"
                VerticalAlignment="Stretch">
                
                <TextBlock Text="{Binding Cardstr}"
                    HorizontalAlignment="Stretch"
                    VerticalAlignment="Stretch"
                    Foreground="{Binding Cardclr}"
                    Background="BlanchedAlmond" />
            </Border>
        </DataTemplate>
    </ListView.ItemTemplate>

</ListView>

before removing an item

我如何修复Listview,这样如果我移除一个项目,它的位置将会有一个空的位置,而不是列表重新排列,如下面的img所示: after removing an item

我试过WrapanelGridview都不能正常工作. 我不想在网格中一个接一个地插入单元格,因为这会使程序复杂化

推荐答案

您应该设置该卡的一个属性,以指示您要显示该特定卡的另一个模板,而不是从源集合中移除该卡.

例如,您可以将IsEmpty属性添加到包含CardstrCardclr属性的类中,然后使用带有DataTriggerContentControl来动态更改模板:

<ListView ItemsSource="{Binding TodoItemViewModels}"
    ScrollViewer.VerticalScrollBarVisibility="Disabled"
    Drop="Card_Drop"
    AllowDrop="True"
    DragLeave="Card_DragLeave"
    IsEnabled="{Binding Disable, ElementName=root}"
    Background="Green">

    <ListView.Resources>
        <DataTemplate x:Key="cardTemplate">
            <Border BorderThickness="2"
                 BorderBrush="BlanchedAlmond" 
                 Opacity="1"
                 Background="BlanchedAlmond"
                 Width="25" Height="30"
                 MouseMove="Card_MouseMove"
                 CornerRadius="5"
                 HorizontalAlignment="Stretch"
                 VerticalAlignment="Stretch">

                <TextBlock Text="{Binding Cardstr}"
                     HorizontalAlignment="Stretch"
                     VerticalAlignment="Stretch"
                     Foreground="{Binding Cardclr}"
                     Background="BlanchedAlmond" />
            </Border>
        </DataTemplate>
        <DataTemplate x:Key="emptyTemplate">
            <Border>
                <TextBlock>empty...</TextBlock>
            </Border>
        </DataTemplate>
    </ListView.Resources>

    <ListView.ItemTemplate>
        <DataTemplate>
            <ContentControl Content="{Binding}">
                <ContentControl.Style>
                    <Style TargetType="ContentControl">
                        <Setter Property="ContentTemplate" Value="{StaticResource cardTemplate}" />
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding IsEmpty}" Value="True">
                                <Setter Property="ContentTemplate" Value="{StaticResource emptyTemplate}" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </ContentControl.Style>
            </ContentControl>
        </DataTemplate>
    </ListView.ItemTemplate>

    <ListView.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel Margin="0" Orientation="Vertical" />
        </ItemsPanelTemplate>
    </ListView.ItemsPanel>

</ListView>

确保您的类实现了INotifyPropertyChanged接口:

public class Card : INotifyPropertyChanged
{
    ...

    private bool _isEmpty;
    public bool IsEmpty
    {
        get { return _isEmpty; }
        set { _isEmpty = value; NotifyPropertyChanged(); }
    }

    public event PropertyChangedEventHandler PropertyChanged; 
    private void NotifyPropertyChanged(string propertyName = "")
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

然后,您只需设置要"删除"的项的IsEmpty属性.

Csharp相关问答推荐

C#自定义字典与JSON(de—)serialize

Unity中的刚体2D运动

自动映射程序在GroupBy之后使用项目

只有第一个LINQ.Count()语句有效

在C#中,有没有一种方法可以集中定义跨多个方法使用的XML参数描述符?

注册所有IMediatR类

当用户右键单击文本框并单击粘贴时触发什么事件?

如何注册类使用多级继承与接口

NET8 MAUI并部署到真实设备上进行测试

如何在C#中转换泛型包装类内部的派生类

将内置的OrderedEumable&Quot;类设置为内部类有什么好处?

如何防止Visual Studio断点以红色突出显示到整行?

升级后发出SWITCH语句

.NET Google Workspace API获取错误CS0266

FakeItEasy自动嘲弄内容

C#中COM对象的实际地址

仅在Blazor Web App中覆盖生产的基本路径(.NET8中的_Hosts.cshtml文件功能?)

在C#中通过Matheval使用自定义公式

如果图表S批注包含使用LINQ的具有特定名称的批注,我如何签入C#

带有类约束的C#泛型