I have xamarin.forms app which contains a List view. I implemented a long press gesture for view cell of list view. Which works fine.What I am trying to do is, inside my list view there is a checkbox and its visibility property is set bind to data model. Defaultly it will be false. If I long pressed the view cell I want all checkbox to be visible.My purpose is multi select the list view.How can I accomplish that?
My data model
public class TimeSheetListData
{
public string StartDate { get; set; }
public string EndDate { get; set; }
public bool Selected { get; set; }
public bool IsCheckBoxVisible { get; set; }
}
I am simply setting the API data to the item source of listview.
ObservableCollection<TimeSheetListData> resultObjForApprovedTimeSheetList = new ObservableCollection<TimeSheetListData>();
After API call,
TimesheetListView.ItemsSource = resultObjForApprovedTimeSheetList;
My Longpress Event and changing the chekbox visiblility.
private void CustomView_LongPressEvent(object sender, EventArgs e)
{
foreach (TimeSheetListData TS in resultObjForApprovedTimeSheetList)
{
TSData.IsCheckBoxVisible = true;
}
TimesheetListView.ItemsSource = null;
TimesheetListView.ItemsSource = resultObjForApprovedTimeSheetList
}
It will change the visiblity of checkbox to true. But it will only visible when listview scrolled.
How can I solve this?
You need to implement INotifyPropertyChanged interface so that we could update the UI in runtime.
model
public class TimeSheetListData: INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public string StartDate { get; set; }
public string EndDate { get; set; }
private bool selected;
public bool Selected {
get
{
return selected;
}
set
{
if (value!= null)
{
selected = value;
NotifyPropertyChanged("Selected");
}
}
}
private bool isCheckBoxVisible;
public bool IsCheckBoxVisible
{
get
{
return isCheckBoxVisible;
}
set
{
if (value != null)
{
isCheckBoxVisible = value;
NotifyPropertyChanged("IsCheckBoxVisible");
}
}
}
}
Related
I am having issues with binding context, after initialisation it doesn't update. so the button is not clickable and the name label doesn't update as its stated in ctor. This is the first and only page.
Page
<Label x:Name="NamesLabels" Text="{Binding Name}"/>
<Button HorizontalOptions="FillAndExpand" Text="Show scanner" Command="{Binding ShowScannerCommand}"/>
public MainPage()
{
InitializeComponent();
}
protected override void OnAppearing()
{
base.OnAppearing();
BindingContext = new MainPageViewModel();
}
//also tried
protected override void OnAppearing()
{
base.OnAppearing();
var context = new MainPageViewModel();
BindingContext = context;
name.Source = context.Name;
btn.Command = context.ShowScannerCommand;
}
ViewModel
public string Name
{
get => _name;
private set
{
_name = value;
NotifyPropertyChanged("Name");
}
}
public ICommand ShowScannerCommand { get; private set; }
public MainPageViewModel()
{
Name = "rwatag";
//have tried _name = "rwatag";
ShowScannerCommand = new Command(() => ShowScanner());
}
void ShowScanner()
{
System.Diagnostics.Debug.WriteLine("result");
}
this is what I get after clicking on button and when debugging the code doesn't get fired
[InputEventReceiver] Slow Input: took 118ms in dispatching, now at finishInputEvent (MotionEvent: event_seq=0, seq=78288, action=ACTION_DOWN)
Resolved pending breakpoint at '/Users/de/Projects/Demo/Demo/View/MainPage.xaml.cs:26,1' to void Demo.MainPage.OnAppearing () [0x00014].
[zygote] Do partial code cache collection, code=61KB, data=59KB
[zygote] After code cache collection, code=61KB, data=59KB
[zygote] Increasing code cache capacity to 256KB
I use your code and it works well on my side. I just add a string _name { get; set; } property and implement the INotifyPropertyChanged interface in MainPageViewModel.
Here is the code example:
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
BindingContext = new MainPageViewModel();
}
}
public class MainPageViewModel : INotifyPropertyChanged
{
string _name { get; set; }
public string Name
{
get => _name;
private set
{
_name = value;
OnPropertyChanged("Name");
}
}
public ICommand ShowScannerCommand { get; private set; }
public MainPageViewModel()
{
Name = "rwatag";
ShowScannerCommand = new Command(() => ShowScanner());
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
void ShowScanner()
{
System.Diagnostics.Debug.WriteLine("result");
}
}
I working in a project with Xamarin Form using C#. I'm trying to create two foreign keys to the same table, using this code:
[Table("Posts")]
public class Post
{
[PrimaryKey]
public long PostID { get; set; }
public string Name { get; set; }
public TypeEntity mode = null;
[ManyToOne(CascadeOperations = CascadeOperation.CascadeInsert)]
public TypeEntity Mode
{
get
{
return mode;
}
set
{
mode = value;
}
}
[ForeignKey(typeof(TypeEntity))]
public long ModeID { get; set; }
public TypeEntity level = null;
[ManyToOne(CascadeOperations = CascadeOperation.CascadeInsert)]
public TypeEntity Level
{
get
{
return level;
}
set
{
level = value;
}
}
[ForeignKey(typeof(TypeEntity))]
public long LevelID { get; set; }
}
[Table("Types")]
public class TypeEntity
{
[PrimaryKey]
public long TypeID { get; set; }
public string Code { get; set; }
public string Name { get; set; }
}
As you can see, the properties Mode and Level are type "TypeEntty", so I need to create the relations.
When I try to insert data, only "Mode" property is inserted ok, "Level" property stay null.
public abstract class BaseStore<T> where T : new()
{
public static SQLiteConnection sql;
public BaseStore()
{
sql = DependencyService.Get<ISQLite>().GetConnection();
Init();
}
public void Init()
{
sql.CreateTable<T>();
}
public void Insert(T entity)
{
sql.InsertWithChildren(entity);
}
}
I switched properties order, I added Level first than Mode, and Level got the value. It means, that SQLIte take only the first property to create the relation.
Does anyone know why this isn't working?
I'm working with SQLite.Net.Core-PCL 3.1.1 and SQLiteNetExtensions 2.1.0.
You have to manually specify which foreign key corresponds to each relation. To do so, pass the ForeignKey parameter in each ManyToOne attribute.
Can't test it right now, but it would probably look something like this:
[ManyToOne(ForeignKey = "ModeID", CascadeOperations = CascadeOperation.CascadeInsert)]
public TypeEntity Mode
(...)
[ManyToOne(ForeignKey = "LevelID", CascadeOperations = CascadeOperation.CascadeInsert)]
public TypeEntity Level
I have a Child entity of Aggregate Entity with one-to-many relation, and in the child, there is a list containing the parent id. the data coming from the front-end is a list of object and if it is no different from what comes to back-end I will do nothing with it. otherwise, I will remove what has been removed and add what has been added to the table.
I am new at EF CORE and I am trying to apply this login in this relation.
if (child.list.SuccessorId == parent.vehicleCategoryId) => ignore;
if(!child.list.contain(parent.vehicleCategoryId)
remove(parent.vehicleCategoryId) => //delete record with vehicleCategoryId
else
add(child)
here is my entities.
public class VehicleCategory : LookupAggregateRoot<VehicleCategory>
{
#region Constructor
private VehicleCategory()
{
_successors = new List<VehicleSuccessorCategory>();
}
#endregion
#region Data
public virtual LocalizedText Name { get; set; }
public virtual long Sequence { get; set; }
private readonly List<VehicleSuccessorCategory> _successors;
public IEnumerable<VehicleSuccessorCategory> Successors
{
get => _successors.AsReadOnly();
set => throw new NotImplementedException();
}
#endregion
#region Behaviour
public void AddSuccessor(VehicleSuccessorCategory entrySuccessorCategory)
{
_successors.Add(entrySuccessorCategory);
}
public void RemoveSuccessor(VehicleSuccessorCategory entrySuccessorCategory)
{
_successors.Remove(entrySuccessorCategory);
}
}
public class VehicleSuccessorCategory : ID365Entity<int>, IEnumerable
{
#region Constructor
public int Id { get; set; }
public int SuccessorId { get; set; }
public VehicleSuccessorCategory(int order)
{
Order = order;
}
#endregion
#region Data
public int Order { get; set; }
#endregion
public bool IsTransient()
{
throw new NotImplementedException();
}
public IEnumerator GetEnumerator()
{
yield return Id;
yield return Order;
}
}
I tried
VehicleCategory vehicleCategory = _genericRepository.Get(aggregate.Id);
foreach (var successorCategory in aggregate.Successors)
{
var successorCategoryToRemove =
vehicleCategory.Successors.Where(e => e.SuccessorId == successorCategory.SuccessorId);
foreach (var vehicleSuccessorCategory in successorCategoryToRemove)
vehicleCategory.RemoveSuccessor(vehicleSuccessorCategory);
}
I am using sqlite-net-pcl and adding a new column to a database DTO and I wanted to set the default value to true and then once I have update the data it would update to the correct value. But the default is not working for me in xamarin.
is there any other way to do this?
[NotNull]
public boolean Istaxable { get; set; } = true;
This will block me from doing a update.
[NotNull, Default(value: true)]
Error default is unknown
DTO
public class DtoTaxableLink
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
[NotNull]
public bool IsTaxable { get; set; } = true;
}
service
await App.LocalDB.InsertTaxableLinksAsync(BuildDbTaxableLinkItem( public Task<int> InsertTaxableLinksAsync(List<DtoTaxableLink> taxableLinks)
ListResponse.Data));
local db
public Task<int> InsertTaxableLinksAsync(List<DtoTaxableLink> taxableLinks)
{
return database.InsertAllAsync(taxableLinks, true);
}
Helper
private static List<DtoTaxableLink> BuildDbTaxableLinkItem(List<TaxablelineLink> taxableLinks)
{
List<DtoTaxableLink> dtoTaxableLink= new List<DtoTaxableLink>();
foreach (var taxink in taxableLinks)
{
DtoTaxableLink dtoTaxableLink= new DtoTaxableLink();
dtoTaxableLink.IsTaxable = taxableLinks.IsTaxable ;
dtoTaxableLink.Add(dtoTaxableLink);
}
return dtoTaxableLink;
}
According to your description, you want to set the default value to true when using sqlite-net-pcl and adding a new column to a database.
You can do it through property itself, field default value not going change until another value going to set.Please take a look the following code:
public class User
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
public string userName { get; set; }
public string password { get; set; }
private bool _sel = true;
[NotNull]
public bool Selected
{
get { return _sel; }
set { _sel = value; }
}
}
Now you can see I set Selected property default value is True, then you can update this value that you want.
I'm trying to get a collection of radio buttons to postback. I'm able to display them on the form just fine, and when i submit i receive it on my controller but the list is empty, what am I doing wrong here?
VIEW-MODEL
public class ProfileViewModel
{
public class FederalClassificationViewModel
{
public int Id { get; set; }
public string Name { get; set; }
public bool IsSelected { get; set; }
}
public IList<FederalClassificationViewModel> federalClassificationsRadioViewModel { get; set; }
}
CONTROLLER
//Retrieve all available radio buttons for tax information form
var allTaxFederalClassList = _taxFederalRepo.GetAllTaxFederalClassesList();
foreach (var federalClass in allTaxFederalClassList)
{
ProfileViewModel.FederalClassificationViewModel federalClassVM = new ProfileViewModel.FederalClassificationViewModel();
federalClassVM.IsSelected = false;
federalClassVM.Name = federalClass.Name;
federalClassVM.Id = federalClass.id;
model.federalClassificationsRadioViewModel.Add(federalClassVM);
}
VIEW
#foreach(var radio in Model.federalClassificationsRadioViewModel)
{
#Html.RadioButtonFor(p=>p.federalClassificationsRadioViewModel, radio) #radio.Name
}
Updated with new Results
#for(int i=0; i<Model.federalClassificationsRadioViewModel.Count; i++)
{
#Html.RadioButtonFor(p=>p.federalClassificationsRadioViewModel[i].IsSelected, Model.federalClassificationsRadioViewModel[i].Id) #Model.federalClassificationsRadioViewModel[i].Name
}
RESULTS
EDIT :
Change your view model like:
public class ProfileViewModel
{
public class FederalClassificationViewModel
{
public int Id { get; set; }
public string Name { get; set; }
public bool IsSelected { get; set; }
}
public int SelectedClass {get;set;}
public IList<FederalClassificationViewModel> federalClassificationsRadioViewModel { get; set; }
}
Change your foreach to for loop:
#for(int i=0; i<Model.federalClassificationsRadioViewModel.Count; i++)
{
#Html.RadioButtonFor(p=>p.
SelectedClass,Model.federalClassificationsRadioViewModel[i].Id) #Model.federalClassificationsRadioViewModel[i].Name
}
You need to understand Model binding of List, Collection and Arrays, you can see details here