2014-09-03 3 views
1

이 목록 상자에는 연락처 이름이 표시됩니다.바인딩 가능한 컬렉션의 항목 색인 가져 오기

<ListBox x:Name="Items" Margin="36,38,78,131"> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
       <StackPanel Orientation="Horizontal"> 
        <TextBlock x:Name="lol" Text="{Binding Path=ContactName}" Style="{StaticResource PhoneTextSmallStyle}" 
Width="Auto" TextAlignment="Center" FontWeight="Bold" Foreground="White" VerticalAlignment="Bottom" TextWrapping="Wrap"/> 
        <Button x:Name="ShowName"> 
         <i:Interaction.Triggers> 
          <i:EventTrigger EventName="Click"> 
           <cal:ActionMessage MethodName="delete" /> 
          </i:EventTrigger> 
         </i:Interaction.Triggers> 
        </Button> 
       </StackPanel> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 

은 내가 접촉하는 경우 그 버튼을 누르면 삭제할 수 있습니다

public List<FBContacts> listContactDatas { get; set; } 

Items = new BindableCollection<FBContacts>();= new BindableCollection<FBContacts>(); 

public void GetContacts() 
    { 
     using(MyDataContext mydb = new MyDataContext(DBConnectionstring)) 
     { 
     var items = from ContactsList Name in mydb._contacts select Name; 
     foreach (var toDoItem in items) 
     { 
      Items.Add(new FBContacts() 
       { 
        ContactName = toDoItem.Name 
       }); 
     } 
     } 
    } 

사용자 로컬 DB에서 연락처를 얻을.

public void delete() 
    { 
     Items.RemoveAt(/* index*/); 
    } 

어떻게 연락처를 선택할 수 있습니까? 별도의 재산

답변

1

그것은 쉽게 에 의해 FBContacts 대신 개체 인덱스 :

public void delete(FBContacts item) 
{ 
    Items.Remove(item); 
} 
1

바인드 현재 선택된 항목의 인덱스 : 물론

<ListBox x:Name="Items" SelectedIndex="{Binding SelectedListIndex}" Margin="36,38,78,131"> 

, SelectedListIndex는 뷰 모델에 PropertyChanged를 발생 유형 int의 속성으로 정의되어야합니다. 그런 다음

, 당신은 쉽게 뷰 모델 내 모든 곳에서 선택한 항목의 인덱스를 액세스 할 수 있습니다

<Button x:Name="ShowName"> 
    <i:Interaction.Triggers> 
     <i:EventTrigger EventName="Click"> 
      <cal:ActionMessage MethodName="delete"> 
       <cal:Parameter Value="{Binding}" /> 
      </cal:ActionMessage> 
     </i:EventTrigger> 
    </i:Interaction.Triggers> 
</Button> 

그런 다음 당신은 제거 할 수 있습니다 : 당신은이 FBContacts 방법 delete에를 클릭 된 전달하면

public void delete() 
{ 
    Items.RemoveAt(SelectedListIndex); 
} 
+0

선택 항목을 변경할 때'SelectedListIndex' 속성이 업데이트됩니까 (예 : Setter의 중단 점)? 어쩌면 당신은'SelectedIndex = "{SelectedListIndex, Mode = TwoWay}"속성 업데이트를 강제로 추가 할 필요가 있습니다. – andreask

+0

이 오류가 발생합니다 System.Windows.ni.dll에서 –

+0

아마도 목록의 선택은 다음과 같습니다. 'SelectedListIndex'가 생성되기 전에 변경된 사항 목록을 채우는 방법 목록 항목은 데이터 바인딩을 통해 설정되어 있습니까? – andreask