私はこのリストボックスを持っています:
<ListBox x:Name="MyList" ItemsSource="{Binding ListOfBullets, Mode=TwoWay, Converter=StaticResourcedebugConverter}}">
<ListBox.ItemTemplate>
<DataTemplate>
<local:TaskStepControl Text="{Binding}" AddHnadler="{Binding DelegateForHandlingAddTaskStep, ElementName=uc}"></local:TaskStepControl>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
バインディングは問題なく動作します。各 local:TaskStepControl には、AddHnadler に接続されている [追加] ボタン があります。AddHnadler は次のようになります。
void AddHnadler(TaskStepControl theControl)
{
// "theControl" --> this TaskStepControl on which the Add button was pressed
//In here I want to get the index of "theControl" in the ListBox "MyList".
//I've tried
var pos = MyList.Items.IndexOf(theControl);
//pos == -1 always
}
各 TaskStepControl の [追加] ボタンが Click イベントを ListBox に渡さないため、SelectionChanged イベントを使用できません。
私は通常、 xaml ではなくコード ビハインドで作業するため、これは非常に単純かもしれませんが、動作させることができません。"IndexOf" のような単純なものが必要で、MVVM のものは必要ありません。通常、xaml ではなくコード ビハインドで作業すると言ったので、今回はこれを実装する必要があります。
ありがとうございました!