Reputation: 3885
inside my listbox, i have rows of a stackpanel.. and in each stack panel is a move row up and move row down button... i'm having trouble coding it though. this is what i have for move up for example:
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;
}
my problem is finding out how to get the "selected index" and selected item" of the row that the button is in.. is this possible?
Upvotes: 1
Views: 725
Reputation: 9857
The simple answer is to get the parent of the button that is clicked. That parent should be your stack panel.
Then, once you have your stack panel you can feed that object into your list boxes get index method. Once you have the index its easy. You just switchem around with a knockoff of the search and switch algarithm.
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 file
/// <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);
}
}
}
}
This code has been compiled and ran so it should be a good starting point for you.
This is the complicated/depricated way to do it. There are less complicated/more professional ways of doing it such as Databinding.
If you REALLY want to be elite look into databinding to a listbox.
Basically what will happen is you create all your objects and put them in a observable collection. Then instead of clearing and re-adding stuff to your listbox manually you can just manipulate the collection.
Simple WPF DataBinding of a ListBox to an ObservableCollection of strings
Upvotes: 1
Reputation: 6948
I think you'll find moving items around in a listbox problematic at best. You'll find it much easier to use a List<> and make that the datasource for your listbox. Everytime the list changes rebuild the listbox.
Upvotes: 0