VB中的依赖属性出错

本文关键字:属性 出错 依赖 VB | 更新日期: 2023-09-27 18:25:55

我很难将IValueConverter的一些C#代码翻译成visualbasic.net,这有助于生成一个编号列表框。我的困难在于LINQ语句,var listBox=lbi。GetVisualAncestors。。。和var index=listBox.ItemContainerGenerator…

有人能帮忙吗?谢谢

  using System;
  using System.Collections.Generic;
  using System.Globalization;
  using System.Linq;
  using System.Windows.Controls;
  using System.Windows.Controls.Primitives;
  using System.Windows.Data;
  namespace RowNumber
  {
     public class ListItemIndexConverter : IValueConverter
     {
        // Value should be ListBoxItem that contains the current record. RelativeSource={RelativeSource AncestorType=ListBoxItem}
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
           var lbi = (ListBoxItem)value;
           var listBox = lbi.GetVisualAncestors().OfType<ListBox>().First();
           var index = listBox.ItemContainerGenerator.IndexFromContainer(lbi);
           // One based. Remove +1 for Zero based array.
           return index + 1;
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); }
     }
     public partial class MainPage : UserControl
     {
        public MainPage()
        {
           // Required to initialize variables
           InitializeComponent();
        }
        public List<string> Test { get { return new[] { "Foo", "Bar", "Baz" }.ToList(); } }
     }
  }

VB中的依赖属性出错

因此,正如我在评论中指出的:)

lbi.GetVisualAncestors().OfType(Of ListBox).First()

应该这样做,是的,我同意它过于冗长:D