【问题标题】:Get index of item in bindable collection获取可绑定集合中项目的索引
【发布时间】:2014-10-27 14:57:55
【问题描述】:

在此列表框中,我显示联系人姓名。

<ListBox x:Name="Items" Margin="36,38,78,131">
    <ListBox.ItemTemplate>
        <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <TextBlock x:Name="lol" Text="{Binding Path=ContactName}" Style="{StaticResource PhoneTextSmallStyle}"
Width="Auto" TextAlignment="Center" FontWeight="Bold" Foreground="White" VerticalAlignment="Bottom" TextWrapping="Wrap"/>
                    <Button x:Name="ShowName">
                        <i:Interaction.Triggers>
                            <i:EventTrigger EventName="Click">
                                <cal:ActionMessage MethodName="delete" />
                            </i:EventTrigger>
                        </i:Interaction.Triggers>
                    </Button>
                </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

我从本地数据库获取联系人

public List<FBContacts> listContactDatas { get; set; }

Items = new BindableCollection<FBContacts>();= new BindableCollection<FBContacts>();

public void GetContacts()
    {
       using(MyDataContext mydb = new MyDataContext(DBConnectionstring))
       { 
        var items = from ContactsList Name in mydb._contacts select Name;
        foreach (var toDoItem in items)
        {
           Items.Add(new FBContacts()
                {
                    ContactName = toDoItem.Name
                });
        }
        }
    }

用户可以删除任何联系人,如果他按下按钮。

public void delete()
    {
        Items.RemoveAt(/* index*/);
    }

那么我怎样才能获得所选联系人的索引?

【问题讨论】:

    标签: c# xaml windows-phone-7 windows-phone-8 caliburn.micro


    【解决方案1】:

    如果将点击的FBContacts 传递给delete 方法会更容易:

    <Button x:Name="ShowName">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="Click">
                <cal:ActionMessage MethodName="delete">
                    <cal:Parameter Value="{Binding}" />
                </cal:ActionMessage>
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </Button>
    

    然后您可以通过FBContacts 对象而不是索引来删除:

    public void delete(FBContacts item)
    {
        Items.Remove(item);
    }
    

    【讨论】:

      【解决方案2】:

      将当前选中项的索引绑定到一个单独的属性:

      <ListBox x:Name="Items" SelectedIndex="{Binding SelectedListIndex}" Margin="36,38,78,131">
      

      当然,SelectedListIndex 必须定义为在 Viewmodel 中触发 PropertyChangedint 类型的属性。

      然后,您可以在 Viewmodel 中的任何位置轻松访问所选项目的索引:

      public void delete()
      {
          Items.RemoveAt(SelectedListIndex);
      }
      

      【讨论】:

      • SelectedListIndex 属性是否在更改选择时更新(例如,设置器上的断点)?可能需要加SelectedIndex="{Binding SelectedListIndex, Mode=TwoWay}"来强制更新属性
      • 我在 System.Windows.ni.dll 中收到此错误 System.Reflection.TargetInvocationException"
      • 可能在创建SelectedListIndex 之前更改了列表的选择。你如何填充列表?列表项是否通过数据绑定设置?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-28
      • 2011-07-11
      • 1970-01-01
      • 2012-02-18
      • 1970-01-01
      • 2016-06-06
      • 2014-10-14
      相关资源
      最近更新 更多