1

在我的列表框中,我有一排堆栈面板。在每个堆栈面板中都有一个上移行和下移行按钮...虽然我在编码时遇到了麻烦。这就是我要向上移动的内容,例如:

            int selectedIndex = ListBoxT10.SelectedIndex;
            if (selectedIndex > 0 && selectedIndex != -1)
            {
                if (ListBoxT10.SelectedItem == null)
                    return;

                var idx = ListBoxT10.SelectedIndex;
                var elem = ListBoxT10.SelectedItem;
                ListBoxT10.Items.RemoveAt(idx);
                ListBoxT10.Items.Insert(idx - 1, elem);
                ListBoxT10.SelectedIndex = idx - 1;
            }

我的问题是找出如何获取按钮所在行的“选定索引”和选定项”。这可能吗?

4

2 回答 2

1

简单的答案是获取被点击按钮的父级。该父级应该是您的堆栈面板。

然后,一旦你有了你的堆栈面板,你就可以将该对象输入到你的列表框获取索引方法中。一旦你有了索引就很容易了。您只需使用仿冒的搜索和切换算法进行切换。

XAML

<Window x:Class="sptest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ListBox Name="mylb">

        </ListBox>
    </Grid>
</Window>

CS文件

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        for (int x = 0; x < 10; x++)
        {
            StackPanel sp = new StackPanel();
            Button upbt = new Button();
            Button dwbt = new Button();
            upbt.Click += bt_Click;
            dwbt.Click+= bt_Click;
            upbt.Tag = "up";
            dwbt.Tag = "down";
            upbt.Content = "Up";
            dwbt.Content = "Down";
            sp.Orientation = Orientation.Vertical;
            sp.Children.Add(upbt);
            sp.Children.Add(dwbt);
            mylb.Items.Add(sp);
        }
    }

    void bt_Click(object sender, RoutedEventArgs e)
    {
        Button but = (sender as Button);
        var par = but.Parent;
        string tag = but.Tag.ToString();

        if (par is StackPanel)
        {
            StackPanel sp = (par as StackPanel);

            int index = mylb.Items.IndexOf(sp);

            List<StackPanel> items = new List<StackPanel>();
            foreach (StackPanel item in mylb.Items)
            {
                items.Add(item);
            }

            if (but.Tag == "up")
            {
                if (index != 0)
                {
                    StackPanel temp = items[index - 1];
                    items[index - 1] = items[index];
                    items[index] = temp;
                }
            }
            else
            {
                if (index != items.Count)
                {
                    StackPanel temp = items[index + 1];
                    items[index + 1] = items[index];
                    items[index] = temp;
                }
            }

            mylb.Items.Clear();

            foreach (StackPanel item in items)
            {
                mylb.Items.Add(item);
            }
        }
    }
}

此代码已编译并运行,因此它应该是您的一个很好的起点。

这是复杂/贬义的方法。有更简单/更专业的方法,例如数据绑定。

如果您真的想成为精英,请查看与列表框的数据绑定。

基本上会发生的是您创建所有对象并将它们放入可观察的集合中。然后,您无需手动清除并重新将内容添加到列表框中,您只需操纵集合即可。

ListBox 到字符串的 ObservableCollection 的简单 WPF 数据绑定

于 2013-10-09T23:27:15.780 回答
0

我认为您会发现在列表框中移动项目充其量是有问题的。您会发现使用 List<> 并将其作为列表框的数据源要容易得多。每次列表更改时都会重建列表框。

于 2013-10-09T23:31:41.843 回答