Silverlight 4和LisBox&x27;当使用DataTemplate作为ItemTemplate时,选择EdItem

Silverlight 4和LisBox&x27;当使用DataTemplate作为ItemTemplate时,选择EdItem,silverlight,silverlight-4.0,listbox,itemtemplate,Silverlight,Silverlight 4.0,Listbox,Itemtemplate,我使用两个列表框进行拖放,用户可以从源列表框中拖动项目并拖放到目标上。我正在为我的ListBox控件的ListBoxItems使用DataTemplate 我需要让用户能够在目标列表框中的项目从源位置移动后向上/向下移动。我有两个按钮“向上移动”和“向下移动”,但是当用户单击其中一个按钮时,它会将空对象作为selectedItem返回给我 这是我的密码 private void moveUp_Click(object sender, RoutedEventArgs e) {

我使用两个列表框进行拖放,用户可以从源列表框中拖动项目并拖放到目标上。我正在为我的ListBox控件的ListBoxItems使用DataTemplate

我需要让用户能够在目标列表框中的项目从源位置移动后向上/向下移动。我有两个按钮“向上移动”和“向下移动”,但是当用户单击其中一个按钮时,它会将空对象作为selectedItem返回给我

这是我的密码

private void moveUp_Click(object sender, RoutedEventArgs e)
    {
      ListBoxItem selectedItem = lstmenuItems.SelectedItem as ListBoxItem;

        if (selectedItem != null)
        {
         int index = lstmenuItems.Items.IndexOf(selectedItem);


            if (index != 0)
            {
                lstmenuItems.Items.RemoveAt(index);
                index -= 1;
                lstmenuItems.Items.Insert(index, selectedItem);
                lstmenuItems.SelectedIndex = index;
            }

        }

    } 
我确信这与ItemTemplate有关。这是listbox的xaml

 <ListBox x:Name="lstmenuItems" Height="300" MinWidth="200" >
    <ListBox.ItemTemplate>
       <DataTemplate>
           <StackPanel Orientation="Horizontal">
                  <StackPanel Orientation="Vertical">
                      <TextBlock Text="{Binding Code}"/>
                      <TextBlock Text="{Binding RetailPrice, StringFormat=£\{0:n\}}" />
                  </StackPanel>
           <!-- Product Title-->
           <TextBlock Text="{Binding Description1}" Width="100"  Margin="2" />
           </StackPanel>
      </DataTemplate>
  </ListBox.ItemTemplate>

您知道如何访问所选项目以及如何上下移动它吗


提前感谢

变量
selectedItem
将包含空值,因为
selectedItem
属性不返回类型
ListBoxItem
SelectedItem
属性返回从集合接收的对象,并提供其
ItemsSource
属性

改为:-

object selectedItem = lstmenuItems.SelectedItem;
这会让你走得更远


< P > >考虑将<代码> ItStaseStase<代码>绑定到<代码> StababCeleCopy< /Cord>并操作集合。<> > >选择项变量将包含NULL,因为<代码> SelectedItem < /C>属性不返回类型<代码> ListBox项目。
SelectedItem
属性返回从集合接收的对象,并提供其
ItemsSource
属性

改为:-

object selectedItem = lstmenuItems.SelectedItem;
这会让你走得更远


这样说,考虑将<代码> ItStaseStase<代码>绑定到ExababelCopys< /Cord>并操作集合。

我已经绑定到BuffelVabcLeCopy,但问题是它没有排序。因此,我想在drop事件中遍历集合,并按用户放入列表的顺序对其进行排序。@Jhelumi:如果您操作ObservableCollection(而不是绑定到它的ListBox)列表框应该更改以反映集合中的更改。我已将其绑定到ObeservableCollection,但问题是它没有排序。因此,我希望在drop事件中遍历集合,并按用户放入列表的顺序对其进行排序。@Jhelumi:如果您操作ObservableCollection(而不是绑定到它的ListBox),则ListBox应该更改以反映集合中的更改。