gpt4 book ai didi

c# - 从 Xamarin 表单中的 Listview 获取选中的值

转载 作者:行者123 更新时间:2023-11-30 21:29:15 24 4
gpt4 key购买 nike

我是 xamarin 和 C# 的新手。我的应用程序中有一个 ListView ,我正在通过 ItemsSource 绑定(bind)数据。我在 ListView 中有一个复选框。我正在使用 Xamarin.Forms.InputKit。我想要的是通过 ViewModel 访问/获取所有选中的行数据。数据绑定(bind)效果很好。我尝试使用 CheckChangedCommand,但它没有在 ViewModel 内部触发。

ListView ....

<ListView ItemsSource="{Binding LifeDemandList}" x:Name="lDemandView" HasUnevenRows="True">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout HeightRequest="44" IsVisible="True" BackgroundColor="White" Orientation="Vertical">

<Grid Padding="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="40"></ColumnDefinition>
</Grid.ColumnDefinitions>
<StackLayout BackgroundColor="White" HorizontalOptions="FillAndExpand" VerticalOptions="CenterAndExpand" Grid.Column="0">
<Label HorizontalOptions="CenterAndExpand" FontFamily="{Binding quicksandsFont}" FontSize="14" FontAttributes="Bold" VerticalOptions="FillAndExpand" TextColor="Black" Text="{Binding DEMAND}"/>
</StackLayout>
<StackLayout BackgroundColor="White" HorizontalOptions="FillAndExpand" VerticalOptions="CenterAndExpand" Grid.Column="1">
<Label HorizontalOptions="CenterAndExpand" FontFamily="{Binding quicksandsFont}" FontSize="14" FontAttributes="Bold" VerticalOptions="FillAndExpand" TextColor="Black" Text="{Binding PREMIUM}"/>
</StackLayout>
<StackLayout BackgroundColor="White" HorizontalOptions="FillAndExpand" VerticalOptions="CenterAndExpand" Grid.Column="2">
<Label HorizontalOptions="CenterAndExpand" FontFamily="{Binding quicksandsFont}" FontSize="14" FontAttributes="Bold" VerticalOptions="FillAndExpand" TextColor="Black" Text="{Binding LATEFEE}"/>
</StackLayout>
<StackLayout BackgroundColor="White" HorizontalOptions="FillAndExpand" VerticalOptions="CenterAndExpand" Grid.Column="3">
<input:CheckBox HorizontalOptions="CenterAndExpand" IsChecked="{Binding ROWCHECK}" Type="Check"/>
</StackLayout>
</Grid>

</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>

通过 ViewModel 进行数据绑定(bind)的示例。

    List<LifeDemandData> lList = new List<LifeDemandData>();

LifeDemandData ldd = new LifeDemandData();
ldd.DEMAND = "2019/03";
ldd.PREMIUM = "2,000.00";
ldd.LATEFEE = "10.00";
ldd.ROWCHECK = false;
lList.Add(ldd);

LifeDemandData ldd1 = new LifeDemandData();
ldd1.DEMAND = "2019/04";
ldd1.PREMIUM = "11,896.00";
ldd1.LATEFEE = ".00";
ldd1.ROWCHECK = false;
lList.Add(ldd1);

LifeDemandList = lList;

最佳答案

解决方案:

可以设置复选框的事件CheckChanged,获取所有选中的行数据。

in xaml

<input:CheckBox  HorizontalOptions="CenterAndExpand" IsChecked="{Binding ROWCHECK , Mode=TwoWay}" Type="Check" CheckChanged="CheckBox_CheckChanged"/>

in code behind

比如我在contentPage中实现

public partial class MainPage : ContentPage
{

public ObservableCollection<LifeDemandData> LifeDemandList { get; set; }
public ObservableCollection<LifeDemandData> selectedList;
public MainPage()
{
InitializeComponent();

BindingContext = this;



LifeDemandList = new ObservableCollection<LifeDemandData>()
{
new LifeDemandData{ DEMAND = "2019/01" , LATEFEE = "22,000" , PREMIUM = "10.00" , ROWCHECK = false },
new LifeDemandData{ DEMAND = "2019/02" , LATEFEE = "11,987" , PREMIUM = ".00" , ROWCHECK = false },
new LifeDemandData{ DEMAND = "2019/03" , LATEFEE = "12,456" , PREMIUM = "6.00" , ROWCHECK = false },
new LifeDemandData{ DEMAND = "2019/04" , LATEFEE = "78,159" , PREMIUM = "3.00" , ROWCHECK = false },

};

listview.ItemsSource = LifeDemandList;

}

private void CheckBox_CheckChanged(object sender, EventArgs e)
{
selectedList = new ObservableCollection<LifeDemandData>();

for (int i=0;i< LifeDemandList.Count;i++)
{
LifeDemandData item = LifeDemandList[i];

if(item.ROWCHECK)
{
selectedList.Add(item);
}
}

DisplayAlert("Title", selectedList.Count + " item have been selected", "Cancel");

// do something you want

}
}

编辑

如果想在ViewModel中实现

in xaml

<input:CheckBox  HorizontalOptions="CenterAndExpand" IsChecked="{Binding ROWCHECK , Mode=TwoWay}" Type="Check" CheckChangedCommand="{Binding CheckedCommand}" />

in model

public class LifeDemandData
{
public string DEMAND { get; set; }
public string PREMIUM { get; set; }
public string LATEFEE { get; set; }
public bool ROWCHECK { get; set; }
public ICommand CheckedCommand { set; get; }

}

in ViewModel

 public class MyViewModel
{

public ObservableCollection<LifeDemandData> LifeDemandList { get; set; }
public ObservableCollection<LifeDemandData> selectedList;

ICommand MyCommand;

public MyViewModel()
{

MyCommand = new Command(() =>
{


selectedList = new ObservableCollection<LifeDemandData>();

for (int i = 0; i < LifeDemandList.Count; i++)
{
LifeDemandData item = LifeDemandList[i];

if (item.ROWCHECK)
{
selectedList.Add(item);
}
}

App.Current.MainPage.DisplayAlert("Title", selectedList.Count + " item have been selected", "Cancel");

});

LifeDemandList = new ObservableCollection<LifeDemandData>()
{
new LifeDemandData{ DEMAND = "2019/01" , LATEFEE = "22,000" , PREMIUM = "10.00" , ROWCHECK = false , CheckedCommand=MyCommand},
new LifeDemandData{ DEMAND = "2019/02" , LATEFEE = "11,987" , PREMIUM = ".00" , ROWCHECK = false , CheckedCommand=MyCommand},
new LifeDemandData{ DEMAND = "2019/03" , LATEFEE = "12,456" , PREMIUM = "6.00" , ROWCHECK = false , CheckedCommand=MyCommand},
new LifeDemandData{ DEMAND = "2019/04" , LATEFEE = "78,159" , PREMIUM = "3.00" , ROWCHECK = false , CheckedCommand=MyCommand },

};

}


}

enter image description here

关于c# - 从 Xamarin 表单中的 Listview 获取选中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55528352/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com