Reputation: 87
I have xamarin.forms.listview control and I have some elements in it. This listview will be populated dynamically. Now on a particular condition, I need to hide one element inside the listview, how can I find it and hide it?
<ListView x:Name="lstBids" Margin="0" HasUnevenRows="True">
<ListView.ItemTemplate >
<DataTemplate>
<ViewCell>
<Frame Margin="0,0,0,5" Padding="0" BackgroundColor="White">
<StackLayout Orientation="Vertical">
<Label Style="{StaticResource Medium}" Margin="10" HorizontalOptions="StartAndExpand" x:Name="lblComments" Text="{Binding Comments}"></Label>
<Frame x:Name="frmHire" BackgroundColor="{StaticResource base}" Padding="10,5" CornerRadius="5" HasShadow="False">
<Label Text="Hire" Style="{StaticResource MediumWhite}"></Label>
<Frame.GestureRecognizers>
<TapGestureRecognizer Tapped="Hire_Clicked"></TapGestureRecognizer>
</Frame.GestureRecognizers>
</Frame>
<Label Style="{StaticResource SmallGray}" HorizontalOptions="EndAndExpand" Margin="0,0,10,0" x:Name="lblDate" Text="{Binding UpdatedDate, StringFormat='{0:MMMM dd, yyyy hh:mm tt}'}"></Label>
</StackLayout>
</Frame>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
frmHire
is the frame I need to hide in all the listview items on a particular condition, how can I achieve it? please help me.
Upvotes: 2
Views: 1190
Reputation: 18861
You don't need to set the name of frame and get it in code behind . As Roubachof said.You can use data-binding to binding the value of IsVisible
.And change it in ViewModel .
in xaml
<Frame IsVisible="{Binding IsVisible}" Padding="10,5" CornerRadius="5" HasShadow="False">
//...
</Frame>
in your model
public class Model : INotifyPropertyChanged
{
public string Comments { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private bool isvisible;
public bool IsVisible
{
get
{
return isvisible;
}
set
{
if (isvisible != value)
{
isvisible = value;
NotifyPropertyChanged();
}
}
}
//...other property
}
And you can set the value of it.For example ,if you want to set all frame isVisible
as false
foreach(var model in MyItems)
{
model.IsVisible = false;
}
MyItems is the ItemsSource
of listview . Don't forget init the property when you init the ItemsSource .Otherwise the value of IsVisible
is false
in default.
Upvotes: 1
Reputation: 3401
Simply bind the IsVisible
property on your frmHire
to the condition on the item of the ItemsSource
.
The parent view model hosting the ObservableCollection
or List
will just have to go through the items and set the condition accordingly.
Upvotes: 1