Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# VB中的依赖属性_C#_Vb.net_Linq_Dependency Properties - Fatal编程技术网

C# VB中的依赖属性

C# VB中的依赖属性,c#,vb.net,linq,dependency-properties,C#,Vb.net,Linq,Dependency Properties,我很难将IValueConverter的一些C#代码翻译成visual basic.net,这有助于生成一个编号的列表框。我的困难在于LINQ语句,var listBox=lbi.GetVisualAncestors。。。var index=listBox.ItemContainerGenerator 有人能帮忙吗?谢谢 using System; using System.Collections.Generic; using System.Globalization; usin

我很难将IValueConverter的一些C#代码翻译成visual basic.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(); } }
     }
  }
使用系统;
使用System.Collections.Generic;
利用制度全球化;
使用System.Linq;
使用System.Windows.Controls;
使用System.Windows.Controls.Primitives;
使用System.Windows.Data;
命名空间行数
{
公共类ListItemIndexConverter:IValueConverter
{
//值应为包含当前记录的ListBoxItem。RelativeSource={RelativeSource AncestorType=ListBoxItem}
公共对象转换(对象值、类型targetType、对象参数、CultureInfo区域性)
{
var lbi=(ListBoxItem)值;
var listBox=lbi.getVisualancetors().OfType().First();
var index=listBox.ItemContainerGenerator.IndexFromContainer(lbi);
//基于一的。对于基于零的数组,删除+1。
收益率指数+1;
}
公共对象ConvertBack(对象值、类型targetType、对象参数、CultureInfo区域性){抛出新的NotImplementedException();}
}
公共部分类主页面:UserControl
{
公共主页()
{
//需要初始化变量
初始化组件();
}
公共列表测试{get{returnnew[]{“Foo”,“Bar”,“Baz”}.ToList();}
}
}

正如我在评论中指出的那样:)


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

你的意思是,你不知道如何在Vb.net中使用泛型?我猜它是(列表框的)类型的
lbi.GetVisualAncestors()。首先()
尽管我必须检查msdn:)@Icepickle你是对的,它是
(T的)
而不是
。VB一定这么冗长吗?呃……是的,当你来自像C#……这样干净的语言时,这有点烦人……谢谢。我不想讨论VB和C的争论。我想我害怕花括号{}!
lbi.GetVisualAncestors().OfType(Of ListBox).First()