WPF 转换器:无法转换“System.Int32”类型的对象;输入“System.Windows.Controls.ListViewItem”

发布于 2025-01-16 05:16:28 字数 1309 浏览 2 评论 0原文

在 GridViewColumn 中,我有以下转换器:

<GridViewColumn Header="Number"
                DisplayMemberBinding="{Binding (ItemsControl.AlternationIndex), 
                                       RelativeSource={RelativeSource AncestorType=ListViewItem},
                                       Converter={StaticResource IndexConverter}, 
                                       ConverterParameter=1}"/>

此列是从 1 开始的自动增量索引,转换器是:

public class IndexConverter : IValueConverter
{
    public object Convert(object value, Type TargetType, object parameter, CultureInfo culture)
    {
        ListViewItem item = (ListViewItem) value;
        ListView listView = ItemsControl.ItemsControlFromItemContainer(item) as ListView;
        int index = listView.ItemContainerGenerator.IndexFromContainer(item);

        if (Int32.TryParse(parameter as string, out int paramValue))
        {
            index += paramValue;
        }

        return index.ToString();
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

我收到以下编译错误:

无法将“System.Int32”类型的对象转换为类型 'System.Windows.Controls.ListViewItem'

因此 xaml 视图崩溃。

In a GridViewColumn I have below converter:

<GridViewColumn Header="Number"
                DisplayMemberBinding="{Binding (ItemsControl.AlternationIndex), 
                                       RelativeSource={RelativeSource AncestorType=ListViewItem},
                                       Converter={StaticResource IndexConverter}, 
                                       ConverterParameter=1}"/>

This column is an auto-incremental index starting from 1 and the converter is:

public class IndexConverter : IValueConverter
{
    public object Convert(object value, Type TargetType, object parameter, CultureInfo culture)
    {
        ListViewItem item = (ListViewItem) value;
        ListView listView = ItemsControl.ItemsControlFromItemContainer(item) as ListView;
        int index = listView.ItemContainerGenerator.IndexFromContainer(item);

        if (Int32.TryParse(parameter as string, out int paramValue))
        {
            index += paramValue;
        }

        return index.ToString();
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

I am getting below compilation error:

Unable to cast object of type 'System.Int32' to type
'System.Windows.Controls.ListViewItem'

So xaml view is crashing.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

煮酒 2025-01-23 05:16:28

ItemsControl.AlternationIndex 不是 ListViewItem 而是 int

如果您的转换器需要的话,请尝试绑定到 ListViewItem 本身:

DisplayMemberBinding="{Binding Path=., 
    RelativeSource={RelativeSource AncestorType=ListViewItem},
    Converter={StaticResource IndexConverter}, 
    ConverterParameter=1}"

ItemsControl.AlternationIndex is not a ListViewItem but an int.

Try to bind to the ListViewItem itself if that's what your converter expects:

DisplayMemberBinding="{Binding Path=., 
    RelativeSource={RelativeSource AncestorType=ListViewItem},
    Converter={StaticResource IndexConverter}, 
    ConverterParameter=1}"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文