【问题标题】:Problems with deleting items from a listbox从列表框中删除项目的问题
【发布时间】:2012-01-12 08:02:13
【问题描述】:

我试图实现的行为是按住一个列表框项目,我应该能够删除该项目。 我已经通过代码创建了列表框。 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">

【问题讨论】:

    标签: windows-phone-7 listbox contextmenu


    【解决方案1】:

    假设您将 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);
    

    【讨论】:

    • 还有一个问题,我怎样才能获得单击上下文菜单的列表项的索引?
    • 在标签中存储对项目的引用。然后,使用 int idx = listBoxHistory.Items.IndexOf(//标签);找到该项目。
    • @alfah 您可能需要重新绘制整个列表 - 当我像现在一样做 ListBox-es 时,我很确定我们刚刚清除了整个列表并读取了所有项目。仅 20 个项目的性能并不可怕
    • 是的,无论如何我不得不重新绘制整个列表.. 虽然我没有检查过 20 多个性能。我已经使用了标签 :D 非常感谢..
    猜你喜欢
    • 1970-01-01
    • 2021-02-04
    • 2020-12-07
    • 2010-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多