2012-01-12 3 views
0

Поведение, которое я пытаюсь достичь, - нажать и удерживать элемент списка, и я должен удалить его. Я создал список через код. В Everylistitem есть стек, который содержит два текстовых блока, то есть дату и строку.Проблемы с удалением элементов из списка

Как я могу получить событие прессования для выбранного списка? Я нигде не использовал привязку.

C#:

for (int i = 0; i < iHistCount; i++) 
{ 
    TextBlock dateText = new TextBlock(); 
    TextBlock durationText = new TextBlock(); 
    TextBlock spacer = new TextBlock(); 
    TextBlock spacer2 = new TextBlock(); 

    dateText.FontFamily = (FontFamily)App.Current.Resources["CicleFina"]; 
    durationText.FontFamily = (FontFamily)App.Current.Resources["CicleFina"]; 

    dateText.FontSize = 25; 
    durationText.FontSize = 25; 

    dateText.TextAlignment = TextAlignment.Right; 
    durationText.TextAlignment = TextAlignment.Left; 

    dateText.VerticalAlignment = System.Windows.VerticalAlignment.Center; 
    durationText.VerticalAlignment = System.Windows.VerticalAlignment.Center; 

    spacer.Width = 30; 
    dateText.Width = 130; 
    spacer2.Width = 50; 
    durationText.Width = 170; 
    DateTime dtHist = pCycMan.GetHistoryDate(i); 
    strDisplay = dtHist.ToString("dd-MMM-yyyy"); 
    dateText.Text = strDisplay; 
    if(condition) 
    { 
     // logic 
     durationText.Text = strDisplay; 
    } 

    StackPanel st = new StackPanel(); 
    st.Height = 50; 
    st.Orientation = System.Windows.Controls.Orientation.Horizontal; 
    st.Children.Add(spacer); 
    st.Children.Add(dateText); 
    st.Children.Add(spacer2); 
    st.Children.Add(durationText); 
    listboxHistory.Items.Add(st); 
} 

XAML

<ListBox x:Name="listboxHistory" Height="280" Canvas.Left="60" Canvas.Top="232" Width="360" Foreground="Gray"> 

ответ

2

Предполагая, что вы используете Silverlight инструментарий для контекстного меню, эта ссылка является хорошим примером, и имеет, как создать меню в коде -за.

http://windowsphonegeek.com/articles/WP7-ContextMenu-in-depth--Part1-key-concepts-and-API

Например здесь:

StackPanel st = new StackPanel(); 
      st.Height = 50; 
      st.Orientation = System.Windows.Controls.Orientation.Horizontal; 
      st.Children.Add(spacer); 
      st.Children.Add(dateText); 
      st.Children.Add(spacer2); 
      st.Children.Add(durationText); 
      listboxHistory.Items.Add(st); 

ContextMenu cm = new ContextMenu(); 
MenuItem delete = new MenuItem() 
    { 
     Header = "Delete", 
     Tag= "Delete" //you could also put the item itself here, would be a good reference 

    }; 
delete.Clicked += //your event handler 
MenuItem edit = new MenuItem() 
    { 
     Header = "Edit", 
     Tag = "Edit", //you could also put the item itself here, would be a good reference 
    }; 
edit.Clicked += //your event handler 


//add the items to the context menu. 
cm.Items.Add(delete); 
cm.Items.Add(edit); 

ContextMenuService.SetContextMenu(st, cm); 
+0

Отлично !! : D jus wat Мне нужно – alfah

+0

jus еще один вопрос: как я могу получить индекс списка, для которого нажато контекстное меню? – alfah

+1

Сохраните ссылку на элемент в теге. Затем используйте int idx = listBoxHistory.Items.IndexOf (// тег); для поиска элемента. –

Смежные вопросы