Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.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# ItemsControl:绑定一个项';索引的属性是什么?_C#_.net_Wpf_Silverlight - Fatal编程技术网

C# ItemsControl:绑定一个项';索引的属性是什么?

C# ItemsControl:绑定一个项';索引的属性是什么?,c#,.net,wpf,silverlight,C#,.net,Wpf,Silverlight,我有一个自定义对象的集合,我想将ItemsControl的index属性绑定到自定义对象中的int属性之一。如何在模板中定义此类绑定?我需要转换器吗?有什么建议吗?谢谢你想做的事没有意义 假设您有一个具有属性(名称、wishedIndex)的自定义对象(wishedIndex为整数或任何其他用于计算所需索引的魔法) 现在您有了几个这样的对象-->几个期望的索引 在您的架构中的某个地方,您做出了错误的设计选择。如果您发布更多代码,我们可以发现第一个问题:ItemsControl没有索引或Selec

我有一个自定义对象的集合,我想将ItemsControl的index属性绑定到自定义对象中的int属性之一。如何在模板中定义此类绑定?我需要转换器吗?有什么建议吗?谢谢你想做的事没有意义

假设您有一个具有属性(名称、wishedIndex)的自定义对象(wishedIndex为整数或任何其他用于计算所需索引的魔法)

现在您有了几个这样的对象-->几个期望的索引


在您的架构中的某个地方,您做出了错误的设计选择。如果您发布更多代码,我们可以发现第一个问题:ItemsControl没有索引或SelectedIndex属性。为此,您需要从选择器派生的东西(如ComboBox、ListBox等)

在这种情况下,您可以使用SelectedValue和SelectedValuePath属性轻松完成所需操作

public class MyCustomObject {
  public int CustomObjectIndex {get;set;}
}

public class ViewModel : INotifyPropertyChanged {
  public IEnumerable<MyCustomObject> Items {get { return something;} }

  // Setting this must raise PropertyChanged.
  public int SelectedIndex {get; set; }
}

<ComboBox ItemsSource={Binding Items}
          SelectedValue={Binding SelectedIndex, Mode=TwoWay}
          SelectedValuePath="CustomObjectIndex" />
公共类MyCustomObject{
public int CustomObjectIndex{get;set;}
}
公共类视图模型:INotifyPropertyChanged{
公共IEnumerable项{get{return something;}}
//设置此项必须引发PropertyChanged。
public int SelectedIndex{get;set;}
}

我们可以看看您尝试做什么的代码吗?SL和WPF需要吗??