【问题标题】:Xamarin Forms refer to controls inside the ListViewXamarin Forms 指的是 ListView 内的控件
【发布时间】:2016-08-31 22:20:12
【问题描述】:

如何引用ListViewViewCell 内的控件:

    <ListView x:Name="classesListView"
              ItemsSource="{Binding List}"
              HasUnevenRows="True" 
              HeightRequest="200" 
              ItemSelected="ListView_OnSelection">
      <ListView.ItemTemplate>
        <DataTemplate>
          <ViewCell Height="50">
            <StackLayout Orientation="Horizontal">
              <Switch x:Name="chooseItem" VerticalOptions="Center"/>
              <Label Text="{Binding ClassName}" VerticalOptions="Center"/>
            </StackLayout>
          </ViewCell>
        </DataTemplate>
      </ListView.ItemTemplate>
    </ListView>

我如何在后面的代码中引用Switch(在ListView_OnSelection方法中)

谢谢

【问题讨论】:

  • 最好使用绑定。请告诉我,你想在Switch处理什么?
  • 如果切换开关,我需要将标签文本添加到数组中

标签: listview xamarin.forms


【解决方案1】:

您可以向模型添加新属性并使用绑定来查看Switch IsToggled

<Switch x:Name="chooseItem" 
        VerticalOptions="Center" 
        IsToggled="{Binding IsSelectedForBeingAddedToArray, Mode=TwoWay}" />

【讨论】:

  • 感谢您的回复,我会试试的
  • 先生,我在我的视图模型上定义了这个属性,下一步你能帮我吗?私人布尔_isSelectedForBeingAddedToArray;公共布尔 IsSelectedForBeingAddedToArray { 获取 { 返回 _isSelectedForBeingAddedToArray; } 设置 { _isSelectedForBeingAddedToArray = 值; OnPropertyChanged(); } }
  • 不在视图模型中,在模型中,您绑定到列表项
  • 它不起作用,如果它是双向模式,当我点击列表视图单元格时应该切换开关,是不是错了?代码: void ListView_OnSelection(object sender, SelectedItemChangedEventArgs e) { var selectedItem = e.SelectedItem as Class; selectedItem.IsSelectedForBeingAddedToArray = !selectedItem.IsSelectedForBeingAddedToArray; }
  • if(selectedItem.IsSelectedForBeingAddedToArray) - 然后将此项目添加到数组中
猜你喜欢
  • 1970-01-01
  • 2015-11-28
  • 2016-11-13
  • 2017-01-05
  • 1970-01-01
  • 2019-03-30
  • 1970-01-01
  • 2019-08-10
  • 1970-01-01
相关资源
最近更新 更多