2011-09-20 2 views
4

각 행에 삭제 단추가있는 목록 상자가 있으므로 삭제 단추를 클릭하면 목록 상자의 클릭 된 색인이 필요하므로 row.how를 삭제할 수 있습니다. 클릭 한 항목의 색인이 표시됩니까? 여기 목록 상자에서 클릭 한 단추의 인덱스를 얻는 방법

내 목록 상자입니다

<ListBox HorizontalAlignment="Left" Name="listBox1" Margin="-3,132,0,0" VerticalAlignment="Top" Width="498" SelectionChanged="listBox1_SelectionChanged"> 

      <ListBox.ItemTemplate> 
       <DataTemplate> 
        <Border BorderThickness="0,1,0,0" BorderBrush="#FFC1BCBC" Width="490"> 
         <Grid Height="70"> 
          <Image Height="50" 
          HorizontalAlignment="Left" 
          Name="image" 
          Stretch="Fill" 
          VerticalAlignment="Top" 
          Width="50" 
          Source="{Binding iconPath}" Margin="8,8,0,0" /> 
          <TextBlock Name="Name" Text="{Binding displayName}" VerticalAlignment="Center" Margin="60,0,0,0" Foreground="Black" FontWeight="SemiBold"></TextBlock> 

          <Button Name="btnDeleteRow" Width="50" Click="btnDeleteDashboard_Click" Margin="390,0,0,0" BorderBrush="Transparent" Style="{StaticResource logoutbtn_style}"> 

          </Button> 
         </Grid> 
        </Border> 
       </DataTemplate> 
      </ListBox.ItemTemplate> 
     </ListBox> 
+2

listboxName.SelectedIndex – MGZero

+0

하는 데 도움이 설명

희망으로이 값을 참조하지만 내가 선택한 싶어요 목록에없는 삭제 버튼을 클릭하면 색인이 생성됩니다. – Sujiz

답변

2

각 행은 삭제 버튼이 있기 때문에, 각 삭제의 "태그"속성에 인덱스를 할당해야 "내가 삭제 버튼을 클릭하면 내가 클릭 한 인덱스가 필요합니다" 버튼을 클릭하면 삭제 버튼을 클릭 할 때마다 목록 상자의 해당 항목의 색인이 표시됩니다.

죄송합니다. 방금 귀하의 wp 태그와 xaml 코드를 보았 기 때문에 제 대답이 잘못되었을 수 있습니다.

+0

태그 아이디어가 좋습니다 – gbianchi

+0

@unruledboy 어떻게 태그 프로를 사용할 수 있습니까? 색인을 얻으 려구요? – Sujiz

+0

@Sujiz 버튼의 태그를 ListBox에있는 셀의 인덱스와 동일하게 설정했습니다. –

5

ListBox가 일부 소스 모음에 대한 데이터 바인딩이 있다고 가정합니까? 이 경우 단추의 DataContext는 바인딩 된 항목 중 하나의 인스턴스가됩니다.

// 예를 들어 당신이 MyDataObject 인스턴스 목록을 결합하면 ...

// create a list 
List<MyDataObject> myDataObjects = CreateTestData(); 

// bind it 
listBox1.ItemSource = myDataObjects; 

... 

// in your click handler 
private void btnDeleteDashboard_Click(object sender, EventArgs args) 
{ 
    // cast the sender to a button 
    Button button = sender as Button; 

    // find the item that is the datacontext for this button 
    MyDataObject dataObject = button.DataContent as MyDataObject; 

    // get the index 
    int index = myDataObjects.IndexOf(dataObject); 
} 
3

더 나은 옵션은 목록 또는 ObservableObject 컬렉션에 목록 상자 데이터 바인딩을하는 것입니다, 다음과 같이 당신은 할 수있다 다음 두 가지 방법으로 "SelectedItem"또는 "SelectedIndex"(내가 선택한 항목을 선호) 속성에 데이터 바인딩합니다.

그러면 Button을 클릭하면 collection.Remove (selecteditemproperty)를 간단하게 호출 할 수 있습니다.

MVVM 또는 iPropertyNotified를 사용하는 경우 백엔드 컬렉션을 변경할 때보기가 자동으로 목록을 업데이트합니다.

자세한 예제가 필요한 경우 알려 주시기 바랍니다. 하지만 기본적으로 :

public ObservableCollection<ItemViewModel> _items; 
    /// <summary> 
    /// A collection for ItemViewModel objects. 
    /// </summary> 
    public ObservableCollection<ItemViewModel> Items 
    { 
     get 
     { 
      return _items; 
     } 
     set 
     { 
      if (value != _items) 
      { 
       _items = value; 
       NotifyPropertyChanged("Items"); 
      } 
     } 
    } 

    private ItemViewModel _listBoxSelectedItem; 
    /// <summary> 
    /// Sample ViewModel property; this property is used in the view to display its value using a Binding 
    /// </summary> 
    /// <returns></returns> 
    public ItemViewModel ListBoxSelectedItem 
    { 
     get 
     { 
      return _listBoxSelectedItem; 
     } 
     set 
     { 
      if (value != _listBoxSelectedItem) 
      { 
       _listBoxSelectedItem = value; 
       NotifyPropertyChanged("ListBoxSelectedItem"); 
      } 
     } 
    } 

는 다음과 같이 목록 상자 바인딩 :

ItemsSource="{Binding Items}" SelectedItem="{Binding ListBoxSelectedItem, Mode=TwoWay}" 

가 그럼 그냥이

관련 문제