Hi ,I'm new to mobile development and Xamarin as well, I wanted to bind the data from the database to gridview based on if condition, using Xamarin forms cross platform.
For Example,
If the time in the databse is "00:00:00.0000000", then while displaying it should nt display anything it should be left blank. Please help me
Thanks in advance
can you share a simple example using the condition for time ,please?
@DemoCloud Create a custom converter class to transfer the data from a source property to a target property.
public class CustomConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { var str = (string)value; // value is the binding data if (str.Length < 3) return "A"; return "B"; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } }
Consume the custom converter in a page.
<ContentPage.Resources> <local:CustomConverter x:Key="_converter"/> </ContentPage.Resources> <ContentPage.Content> <ListView ItemsSource="{Binding Models}"> <ListView.ItemTemplate> <DataTemplate> <ViewCell> <StackLayout> <Label Text="{Binding content,Converter={StaticResource _converter}}"/> </StackLayout> </ViewCell> </DataTemplate> </ListView.ItemTemplate> </ListView> </ContentPage.Content>
Page.xaml.cs
public partial class Page4 : ContentPage { public ObservableCollection<Model_4> Models { get; set; } public Page4() { InitializeComponent(); Models = new ObservableCollection<Model_4>(); Models.Add(new Model_4 { content = "content_1" }); Models.Add(new Model_4 { content = "content_2" }); Models.Add(new Model_4 { content = "content_3" }); Models.Add(new Model_4 { content = "co" }); BindingContext = this; } }
Check the tutorial:
https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/data-binding/converters
Answers
@DemoCloud You need to make use of Converters.
Have a look at this:
https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/data-binding/converters
@AnubhavRanjan ,can you share a simple example using the condition for time ,please?
I wanted to bind the gridview item based on if else condition,please help.
I've gone through the above example
Thanks in advance
@DemoCloud Create a custom converter class to transfer the data from a source property to a target property.
Consume the custom converter in a page.
Page.xaml.cs
Check the tutorial:
https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/data-binding/converters
@AnubhavRanjan ,
Getting error as "specified cast is not valid"
below is the code
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
please help
Thanks
What type of property did you use to set data binding? The string type? The 'value' is object type, convert it to the type you specified in the model class.
@YelinZh I'm still getting the error, please help me,Below is the updated code.
public class Class1: IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var date = (DateTime)value;
DateTime dResult = DateTime.ParseExact("00:00:00.0000000", "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture);
if (date == dResult)
return "";
return "B";
Page3.xaml
<ContentPage.Resources>
</ContentPage.Resources>
Page3.xaml.cs
public Page3()
{
InitializeComponent();
and also while returning the value instead of "B" i want to return it from the database. In the database if the time is not selected then by default it will be stored as 00:00:00.0000000
Please help
@DemoCloud Try to use the following constructor method to create the dateTime for the binding property. Then get the time value from the binding property and compare it with the default value.
Check the code:
Page.xaml
Page.xaml.cs
Custom converter class