gpt4 book ai didi

wpf - 如何在ListView中显示行号?

转载 作者:行者123 更新时间:2023-12-03 10:21:34 26 4
gpt4 key购买 nike

显而易见的解决方案是在ModelView元素上具有行号属性,但是缺点是在添加记录或更改排序顺序时必须重新生成那些属性。

是否有优雅解决方案?

最佳答案

我认为您有一个优雅的解决方案,但这是可行的。

XAML:

<ListView Name="listviewNames">
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn
Header="Number"
DisplayMemberBinding="{Binding RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type ListViewItem}},
Converter={StaticResource IndexConverter}}" />
<GridViewColumn
Header="Name"
DisplayMemberBinding="{Binding Path=Name}" />
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>

值转换器:
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);
return index.ToString();
}

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

关于wpf - 如何在ListView中显示行号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/660528/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com