【问题标题】:Getting ListView item when clicking "Cell control" like button单击“单元格控件”之类的按钮时获取ListView项目
【发布时间】:2019-10-04 22:24:24
【问题描述】:

我在Cell 中有ListViewButton。当我单击单元格的按钮时,我想获取当前项目。

这是列表视图

<ListView x:Name="list1" ItemsSource="{Binding StudentList}">
        <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <StackLayout>
                    <Image x:Name="Image1"  Source="other.png" />
                    <Label TextColor="{StaticResource mainColor}" 
                           Text="{Binding StudentName}" />
                    <Button x:Name="mybtn"                                
                    BindingContext="{Binding Source={x:Reference list1}, Path=BindingContext}" 
                    BackgroundColor="{DynamicResource CaribGreenPresent}"
                    Text="{Binding AttendanceTypeStatusIdGet, Converter={x:StaticResource IDToStringConverter}}">
                    </Button>
                </StackLayout>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

当我点击mybtn我想在 ViewModel 中获取当前项目时,我该怎么做?

这是我的 ViewModel 代码

private List<Student> _studentList;
public List<Student> StudentList
{
    get { return _studentList; }
    set { SetProperty(ref _studentList, value); }
}

列表截图:

编辑 1: 在 ViewModel 中出现错误:

参数 1:无法从“方法组”转换为“操作”

这是代码

//Constructor
public StudentAttendanceListPageViewModel(INavigationService _navigationService):base(_navigationService)
{
    ItemCommand=new DelegateCommand<Student>(BtnClicked);
}

public DelegateCommand<Student> ItemCommand { get; }
public void BtnClicked(object sender, EventArgs args)
{
    var btn = (Button)sender;
    var item = (Student)btn.CommandParameter;
    // now item points to the Student selected from the list
}

按钮 XAML

<Button x:Name="mybtn"                                
BindingContext="{Binding Source={x:Reference list1}, Path=BindingContext}" 
Command="{Binding ItemCommand }" 
CommandParameter="{Binding Source={x:Reference mybtn}}"              
Text="{Binding AttendanceTypeStatusId, Converter={x:StaticResource IDToStringConverter}}">
</Button>

错误截图:

【问题讨论】:

  • 为什么不把命令放在学生的视图模型上?
  • ItemCommand 仅在 StudentViewModel 内。
  • 那么为什么不直接使用this 来获得点击的StudentViewModel
  • @Haukinger - 很抱歉,我完全不理解。您能否将其写为答案或解释更多。谢谢。

标签: xamarin mvvm xamarin.forms prism


【解决方案1】:

首先,不要更改按钮的BindingContext

<Button Clicked="BtnClicked" CommandParameter="{Binding .}" ... />

然后在你的代码后面

protected void BtnClicked(object sender, EventArgs args)
{
  var btn = (Button)sender;
  var item = (Student)btn.CommandParameter;
  // now item points to the Student selected from the list
}

【讨论】:

  • 感谢您的回答,但这看起来像是不是 Prism 背后的代码。
  • 如何修改此项并与 ViewModel 的List&lt;Student&gt;同步
  • 如果你想避免使用后面的代码,你可以用命令做同样的事情。无论哪种方式,您都会获得对 ViewModel 中存在的相同项目的引用
  • 如果我愿意使用命令而不是在 DelegateCommand 中传递两个参数可能会很困难 object sender, EventArgs args
  • 你不需要发送两个参数,你可以直接用 ICommand 发送 CommandParameter
【解决方案2】:

您可以通过命令绑定来做到这一点:

<Page x:Name="ThePage"
  ...>
  <ListView>
    <ListView.ItemTemplate>
      <DataTemplate>
        <ViewCell>
          ...
          <Button Command="{Binding Source={x:Reference ThePage}, Path=BindingContext.ItemCommand}"
                  CommandParameter="{Binding .}" />
        </ViewCell>
      </DataTemplate
    </ListView.ItemTemplate>
  </ListView>
</Page>

// Now in the VM...
ItemCommand = new DelegateCommand<Student>(ButtonClicked);
private void ButtonClicked(Student student)
{
  // Do something with the clicked student...
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-27
    • 2011-08-23
    • 2014-04-24
    • 1970-01-01
    相关资源
    最近更新 更多