WPF:资源;IndexConverter”;无法解决

本文关键字:解决 IndexConverter 资源 WPF | 更新日期: 2023-09-27 18:20:35

我正试图将ListView的索引绑定到列表中的一列以进行显示。

最后,我还希望能够使用行的DataTemplate中的"删除"按钮删除条目,但使绑定工作是第一步。

这(或一些变体)似乎是最受欢迎的答案之一:如何在ListView中显示行号?

以下是我目前所掌握的:XAML:

<GridViewColumn Header="Index" DisplayMemberBinding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListViewItem}}, Converter={StaticResource IndexConverter}}">
</GridViewColumn>

C#:

public class IndexConverter : IValueConverter
{
    public object Convert(object value, Type TargetType, object parameter)
    {
        ListViewItem item = (ListViewItem)value;
        ListView listView = ItemsControl.ItemsControlFromItemContainer(item) as ListView;
        int index = listView.ItemContainerGenerator.IndexFromContainer(item);
        return index.ToString();
    }
    public object ConvertBack(object value, Type targetType, object parameter)
    {
        throw new NotImplementedException();
    }
}

我将该函数放在CS文件中以供我的应用程序视图使用。

它告诉我它无法解析IndexConverter的资源。我觉得这可能是一个快速解决方案,我要么把函数放错了位置,要么缺少添加资源的代码行。然而,链接中的解决方案并没有解决这个问题。

更新:

根据sa_ddam213的建议,我编辑了我的App.xaml文件,包括以下内容:

<Application x:Class="WpfApplication1.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:myConverters="clr-namespace:WpfApplication1.Views"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary Source="Themes/DarkTheme.xaml" />
        <myConverters:IndexConverter x:Key="IndexConverter" />
    </Application.Resources>
</Application>

我现在收到一个错误,说资源只能设置一次。注释掉DarkTheme消除了这个错误,但它说IndexConverter不存在于WpfApplication1.Views命名空间中,这肯定是我放它的地方。

WPF:资源;IndexConverter”;无法解决

我在最近的一个项目中完成了这项工作。我有一个ListView,我希望用户能够在其中重新排序项目(使用箭头键和/或按钮上下移动条目)。

我向集合的类添加了一个名为SortOrder的新成员,为添加的每个新项分配了一个唯一的值。然后,我编写了一个基于SortOrder对集合项进行排序的函数,以及将项向上或向下移动一个空格的函数。

然后可以将SortOrder属性绑定到列并显示在列中,因为它是类的成员。

这并不完全是我在最初的项目中试图做的,因为我基本上用自己的系统绕过了实际的集合索引,但就用户而言,它确实完成了任务。

public class ViewTest : BindableBase
{
    public long SortOrder { get; set; }
}

private ObservableCollection<ViewTest> testViewList = new ObservableCollection<ViewTest>();
public ObservableCollection<ViewTest> TestViewList
{
    get { return this.testViewList; }
    private set { this.SetProperty<ObservableCollection<ViewTest>>(ref this.testViewList, value); }
}

<ListView ItemsSource="{Binding Path=TestViewList, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Order" DisplayMemberBinding="{Binding Path=SortOrder}" />
        </GridView>
    </ListView.View>
</ListView>

private void AddTest(string test)
{
    ViewTest newTest = new ViewTest();
    newTest.SortOrder = this.GetHighestSortOrder(this.TestViewList) + 1;
    this.TestViewList.Add(newTest);
}

private int GetHighestSortOrder(ObservableCollection<ViewTest> testList)
{
    int highest = 0;
    foreach (ViewTest test in testList)
    {
        if (test.SortOrder > highest)
        {
            highest = Convert.ToInt32(test.SortOrder);
        }
    }
    return highest;
}

private ObservableCollection<ViewTest> SortTests(ObservableCollection<ViewTest> testList)
{
    ObservableCollection<ViewTest> sortedList = new ObservableCollection<ViewTest>();
    while (testList.Count > 0)
    {
        int index = 0;
        int lowest = 0;
        while (index < testList.Count)
        {
            if (testList[index].SortOrder < testList[lowest].SortOrder)
            {
                lowest = index;
            }
            index += 1;
        }
        sortedList.Add(testList[lowest]);
        testList.RemoveAt(lowest);
    }
    return sortedList;
}

向上和向下移动项目的函数只是交换两个SortOrder值,然后调用SortOrder函数来重新排列索引。