[英]ItemsControl: Binding an item's property to the index?
我有一组自定义对象,我想将 ItemsControl 的 index 属性绑定到我的自定义 object 中的 int 属性之一。 我如何在模板中定义这样的绑定? 我需要转换器吗? 有什么建议么? 谢谢
第一个问题:ItemsControl 没有 Index 或 SelectedIndex 属性。 为此,您需要从 Selector 派生的东西(如 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" />
你想做什么没有意义...
假设您有一个自定义 object 属性(名称,wishedIndex)(wishedIndex 为 integer 或任何其他魔法来评估希望的索引)
现在你有几个这样的对象 --> 几个希望的索引。
在您的架构中的某个地方,您做出了错误的设计选择。 如果您发布更多代码,我们可以找到
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.