2011-06-10 42 views
0

我有一個自定義對象的集合,我想將ItemsControl的index屬性綁定到我的自定義對象中的一個int屬性。我如何在模板中定義這種綁定?我需要轉換器嗎?有什麼建議麼?感謝ItemsControl:將項目的屬性綁定到索引?

+0

我們可以看到你想要做的一些代碼嗎? – harryovers 2011-06-10 08:16:44

+0

你需要SL和WPF嗎? – TerenceJackson 2011-06-10 08:50:33

回答

0

你想做的事沒有道理的...

想象你有屬性(名稱,wishedIndex)的自定義對象(如wishedIndex整數或任何其他魔術評估希望指數)

,現在你有幾個這樣的對象 - >幾個希望的索引。

在您的架構中某處,您做出了糟糕的設計選擇。如果您發佈更多代碼,我們可以找出

1

第一個問題:ItemsControl沒有索引或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" /> 
相關問題