2014-10-07 70 views
0

我很难转换为visual basic .net一些C#代码的IValueConverter,它有助于生成一个编号的列表框。我的困难在于LINQ语句,var listBox = lbi.GetVisualAncestors ...和var index = listBox.ItemContainerGenerator ....在VB中依赖属性难倒

任何人都可以帮忙吗?谢谢。

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(); } } 
    } 
    } 
+1

你的意思是,你不”不知道如何在Vb.net中使用泛型?我猜想它是'lbi.GetVisualAncestors()。OfType(Of ListBox).First()'虽然我将不得不检查msdn的:) – Icepickle 2014-10-07 23:21:54

+0

@Ippickle你是对的,它是'(Of T)'而不是''。 VB必须如此详细吗?呃...... – TyCobb 2014-10-07 23:26:23

+0

是的,当你从C#这样干净的语言来时,它有点烦人...... – Icepickle 2014-10-07 23:30:43

回答

1

所以,我的意见:)

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

应该做的伎俩,是的,我同意它指出是过于冗长:d

+0

谢谢。我不会进入VB v C#参数。猜猜我对花括号{}感到恐惧! – user2250708 2014-10-08 03:31:53