I have a collection of custom objects and i want to bind the index property of the ItemsControl to one of the int property in my custom object. how do i define such binding in the template? do i need a conv开发者_开发知识库erter? any suggestions? thanks
First problem: ItemsControl doesn't have an Index or SelectedIndex property. For that, you need something that derives from Selector (like ComboBox, ListBox, etc.).
In that case, you can accomplish what you want easily using the SelectedValue and SelectedValuePath properties.
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" />
what you want to do doesnt make sense...
imagine you have a custom object with properties (name, wishedIndex) (wishedIndex as integer or whatever other magic to evaluate the wished index)
and now you have several of these objects --> several of wished indices.
Somewhere in your architecture you made a bad design choice. if you post more code we can find out
精彩评论