当用户选中网格中的复选框时,如何显示对象?数据在网格中的集合视图中。处理按钮也在下面的网格中。即使我使用MVVM也可以做到吗?
例如,他们选择了任何:
当没有选择项目时,“处理”按钮将显示和隐藏。下面是XAML
<StackLayout >
<!--<SearchBar Placeholder="Search..."/>-->
<RefreshView x:DataType="local:ShpAgentMainPageViewModel" Command="{Binding LoadReleaseDocumentsSA}"
IsRefreshing="{Binding IsRefreshing ,Mode=OneWay}" RefreshColor="#FFFF7F50">
<CollectionView x:Name="DeliveredList"
ItemsSource="{Binding DeliveredDocuments}"
SelectionMode="None" >
<CollectionView.ItemTemplate>
<DataTemplate>
<StackLayout Padding="3" x:DataType="model:Deliver">
<Grid HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
...
</Grid>
</StackLayout>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</RefreshView>
<Grid x:Name="BelowMenu">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Button
CornerRadius="5"
Grid.Row="0"
Text="PROCESS"
FontAttributes="Bold"
BackgroundColor="#FF7F50"
TextColor="White"
WidthRequest="100"
HeightRequest="70"
Margin="20,0,20,22"
HorizontalOptions="End"
VerticalOptions="End"
Command="{Binding TMPendingDetailsPageShpAgent}"
/>
</Grid>
</StackLayout>
对于我的ViewModel:
public class ShpAgentMainPageViewModel : BaseViewModel
{
public Command<Deliver> TMPendingDetailsPageShpAgent { get; set; }
public ShpAgentMainPageViewModel()
{
try
{
TMPendingDetailsPageShpAgent = new Command<Deliver>(OnTMPendingTransferPageShpAgentTap);
}
catch(Exception ex)
{
Debug.WriteLine(ex);
Debug.WriteLine(ex.ToString());
}
}
async void OnTMPendingTransferPageShpAgentTap(Deliver Book)
{
IEnumerable<String> selectedData = DeliveredDocuments.Where(d => d.IsSelected).Select(d => d.TMNo).ToArray();
if (selectedData.Count() == 0)
return;
string tmNo = string.Join("|", selectedData);
try
{
...
}
catch (Exception ex)
{
Debug.WriteLine(ex.ToString());
}
}
}
2条答案
按热度按时间v7pvogib1#
由于我们无法看到您项目的其他详细代码(如
Deliver
),但您可以参考以下实现类似功能的代码。MeetAWalkerViewModel.cs
MainPage.xaml
MainPage.xaml.cs
PetProfile.cs
如需详细信息,您可以检查thread:Count selected checkboxes in collectionview xamarin。
0yycz8jy2#
Sure it can be done.
In Xamarin MVVM you should use bindings for things like these. You can use these in a few variations:
For your use case you have two options:
Set
SelectionMode="Multiple"
to use the built in selection mechanics of theCollectionView
.CollectionView
has a propertySelectedItems
, see: doc . Bind theIsVisible
property of the process button to theCollectionView
'sSelectedItems
. Linking to the right object is the difficult part. Use theSource
property on the binding, to make sure it looks for the object in the right place (view, and not view model). Pay attention to the debug messages at runtime, these will tell you when a binding is failing.Then do the type conversion with a
IsNotEmptyConverter
. This takes yourList
, does something likereturn list.Count > 0
.CollectionView
if you're not using the built-in mechanics for selection.StackLayout
withItemSource
is the most simple option. Bind theIsVisible
property of the process button to your viewmodel'sDeliveredDocuments
. Then write aAnyDocumentIsSelected
converter that takes aList<DeliveredDocuments>
and returns true if any of them have aIsSelected
that istrue
.