Xamarin Forms: Binding 2 properties to another one
up vote
0
down vote
favorite
I have a HorizontalListView (from SharpNado) and I have some buttons to navigate between the different items. I want these buttons only to be visible under certain conditions(they can both be visible at the same time):
If the current index of the listview is greater than 0, the "previous" button should be visible. If the current index of the listview is lower than the highest index (count - 1) I want the "next" button to be visible.
I want to use some binding between views (with converters) to solve this. I have managed to bind the "previous" button's visibility to the current index with a converter that checks if the index is greater than 0, but I'm struggling a lot with the "next" button since it needs to change when either the current index or the amount of items in te list changes.
I've tried a lot of stuff with converterparameters, but I can't bind through them. I've also tried an option where the converter had a binding, but that wouldn't even build.
This is the HorizontalListView I'm using, and at this point it's too late too switch to an alternative, since I'm too far in to this.
At this point I'm really not certain what to do and I'm hoping someone here can help me. Thanks in Advance.
EDIT:
This is the control I'm trying to build.
c# xamarin data-binding xamarin.forms
add a comment |
up vote
0
down vote
favorite
I have a HorizontalListView (from SharpNado) and I have some buttons to navigate between the different items. I want these buttons only to be visible under certain conditions(they can both be visible at the same time):
If the current index of the listview is greater than 0, the "previous" button should be visible. If the current index of the listview is lower than the highest index (count - 1) I want the "next" button to be visible.
I want to use some binding between views (with converters) to solve this. I have managed to bind the "previous" button's visibility to the current index with a converter that checks if the index is greater than 0, but I'm struggling a lot with the "next" button since it needs to change when either the current index or the amount of items in te list changes.
I've tried a lot of stuff with converterparameters, but I can't bind through them. I've also tried an option where the converter had a binding, but that wouldn't even build.
This is the HorizontalListView I'm using, and at this point it's too late too switch to an alternative, since I'm too far in to this.
At this point I'm really not certain what to do and I'm hoping someone here can help me. Thanks in Advance.
EDIT:
This is the control I'm trying to build.
c# xamarin data-binding xamarin.forms
Please share your code and image of your View you want to achieve. It sounds like Next Previous buttons like in Carousel view?
– Nirmal Subedi
Nov 22 at 15:56
@NirmalSubedi, I've posted an image of what I hope to accomplish and it's indeed a kind of carousel. I'm not sure what code to post though.
– Dennis van Opstal
Nov 23 at 7:34
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a HorizontalListView (from SharpNado) and I have some buttons to navigate between the different items. I want these buttons only to be visible under certain conditions(they can both be visible at the same time):
If the current index of the listview is greater than 0, the "previous" button should be visible. If the current index of the listview is lower than the highest index (count - 1) I want the "next" button to be visible.
I want to use some binding between views (with converters) to solve this. I have managed to bind the "previous" button's visibility to the current index with a converter that checks if the index is greater than 0, but I'm struggling a lot with the "next" button since it needs to change when either the current index or the amount of items in te list changes.
I've tried a lot of stuff with converterparameters, but I can't bind through them. I've also tried an option where the converter had a binding, but that wouldn't even build.
This is the HorizontalListView I'm using, and at this point it's too late too switch to an alternative, since I'm too far in to this.
At this point I'm really not certain what to do and I'm hoping someone here can help me. Thanks in Advance.
EDIT:
This is the control I'm trying to build.
c# xamarin data-binding xamarin.forms
I have a HorizontalListView (from SharpNado) and I have some buttons to navigate between the different items. I want these buttons only to be visible under certain conditions(they can both be visible at the same time):
If the current index of the listview is greater than 0, the "previous" button should be visible. If the current index of the listview is lower than the highest index (count - 1) I want the "next" button to be visible.
I want to use some binding between views (with converters) to solve this. I have managed to bind the "previous" button's visibility to the current index with a converter that checks if the index is greater than 0, but I'm struggling a lot with the "next" button since it needs to change when either the current index or the amount of items in te list changes.
I've tried a lot of stuff with converterparameters, but I can't bind through them. I've also tried an option where the converter had a binding, but that wouldn't even build.
This is the HorizontalListView I'm using, and at this point it's too late too switch to an alternative, since I'm too far in to this.
At this point I'm really not certain what to do and I'm hoping someone here can help me. Thanks in Advance.
EDIT:
This is the control I'm trying to build.
c# xamarin data-binding xamarin.forms
c# xamarin data-binding xamarin.forms
edited Nov 22 at 16:08
asked Nov 22 at 15:08
Dennis van Opstal
779728
779728
Please share your code and image of your View you want to achieve. It sounds like Next Previous buttons like in Carousel view?
– Nirmal Subedi
Nov 22 at 15:56
@NirmalSubedi, I've posted an image of what I hope to accomplish and it's indeed a kind of carousel. I'm not sure what code to post though.
– Dennis van Opstal
Nov 23 at 7:34
add a comment |
Please share your code and image of your View you want to achieve. It sounds like Next Previous buttons like in Carousel view?
– Nirmal Subedi
Nov 22 at 15:56
@NirmalSubedi, I've posted an image of what I hope to accomplish and it's indeed a kind of carousel. I'm not sure what code to post though.
– Dennis van Opstal
Nov 23 at 7:34
Please share your code and image of your View you want to achieve. It sounds like Next Previous buttons like in Carousel view?
– Nirmal Subedi
Nov 22 at 15:56
Please share your code and image of your View you want to achieve. It sounds like Next Previous buttons like in Carousel view?
– Nirmal Subedi
Nov 22 at 15:56
@NirmalSubedi, I've posted an image of what I hope to accomplish and it's indeed a kind of carousel. I'm not sure what code to post though.
– Dennis van Opstal
Nov 23 at 7:34
@NirmalSubedi, I've posted an image of what I hope to accomplish and it's indeed a kind of carousel. I'm not sure what code to post though.
– Dennis van Opstal
Nov 23 at 7:34
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
In your ViewModel
add a getter property:
public bool IsLowerThanTheHighestIndex => this.MyIndex < this.MyList.Count - 1;
Then bind the button visibility to that property.
Then raise your changes where you need to:
private int _myIndex;
public int MyIndex
{
get
{
return this._myIndex;
}
set
{
this._myIndex = value;
OnPropertyChanged(nameof(MyIndex));
OnPropertyChanged(nameof(IsLowerThanTheHighestIndex));
}
}
and everywhere you modify MyList
also raise the change, e.g.:
this.MyList.Add(...);
OnPropertyChanged(nameof(IsLowerThanTheHighestIndex));
If you are using an ObservableCollection<T>
you can subscribe to the CollectionChanged
event and call OnPropertyChanged(nameof(IsLowerThanTheHighestIndex));
in the event callback (remember to unsubscribe the event at the end)
HIH
I had such a solution which worked, but I was curious if there was a way to do it without putting extra code in my ViewModel
– Dennis van Opstal
Nov 23 at 7:27
The other way could be to make a custom control in each platform (or use some sort of custom carrousel) that handles the logic of when the buttons are available and you just bind the command of the buttons
– fmaccaroni
Nov 23 at 13:08
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
In your ViewModel
add a getter property:
public bool IsLowerThanTheHighestIndex => this.MyIndex < this.MyList.Count - 1;
Then bind the button visibility to that property.
Then raise your changes where you need to:
private int _myIndex;
public int MyIndex
{
get
{
return this._myIndex;
}
set
{
this._myIndex = value;
OnPropertyChanged(nameof(MyIndex));
OnPropertyChanged(nameof(IsLowerThanTheHighestIndex));
}
}
and everywhere you modify MyList
also raise the change, e.g.:
this.MyList.Add(...);
OnPropertyChanged(nameof(IsLowerThanTheHighestIndex));
If you are using an ObservableCollection<T>
you can subscribe to the CollectionChanged
event and call OnPropertyChanged(nameof(IsLowerThanTheHighestIndex));
in the event callback (remember to unsubscribe the event at the end)
HIH
I had such a solution which worked, but I was curious if there was a way to do it without putting extra code in my ViewModel
– Dennis van Opstal
Nov 23 at 7:27
The other way could be to make a custom control in each platform (or use some sort of custom carrousel) that handles the logic of when the buttons are available and you just bind the command of the buttons
– fmaccaroni
Nov 23 at 13:08
add a comment |
up vote
1
down vote
In your ViewModel
add a getter property:
public bool IsLowerThanTheHighestIndex => this.MyIndex < this.MyList.Count - 1;
Then bind the button visibility to that property.
Then raise your changes where you need to:
private int _myIndex;
public int MyIndex
{
get
{
return this._myIndex;
}
set
{
this._myIndex = value;
OnPropertyChanged(nameof(MyIndex));
OnPropertyChanged(nameof(IsLowerThanTheHighestIndex));
}
}
and everywhere you modify MyList
also raise the change, e.g.:
this.MyList.Add(...);
OnPropertyChanged(nameof(IsLowerThanTheHighestIndex));
If you are using an ObservableCollection<T>
you can subscribe to the CollectionChanged
event and call OnPropertyChanged(nameof(IsLowerThanTheHighestIndex));
in the event callback (remember to unsubscribe the event at the end)
HIH
I had such a solution which worked, but I was curious if there was a way to do it without putting extra code in my ViewModel
– Dennis van Opstal
Nov 23 at 7:27
The other way could be to make a custom control in each platform (or use some sort of custom carrousel) that handles the logic of when the buttons are available and you just bind the command of the buttons
– fmaccaroni
Nov 23 at 13:08
add a comment |
up vote
1
down vote
up vote
1
down vote
In your ViewModel
add a getter property:
public bool IsLowerThanTheHighestIndex => this.MyIndex < this.MyList.Count - 1;
Then bind the button visibility to that property.
Then raise your changes where you need to:
private int _myIndex;
public int MyIndex
{
get
{
return this._myIndex;
}
set
{
this._myIndex = value;
OnPropertyChanged(nameof(MyIndex));
OnPropertyChanged(nameof(IsLowerThanTheHighestIndex));
}
}
and everywhere you modify MyList
also raise the change, e.g.:
this.MyList.Add(...);
OnPropertyChanged(nameof(IsLowerThanTheHighestIndex));
If you are using an ObservableCollection<T>
you can subscribe to the CollectionChanged
event and call OnPropertyChanged(nameof(IsLowerThanTheHighestIndex));
in the event callback (remember to unsubscribe the event at the end)
HIH
In your ViewModel
add a getter property:
public bool IsLowerThanTheHighestIndex => this.MyIndex < this.MyList.Count - 1;
Then bind the button visibility to that property.
Then raise your changes where you need to:
private int _myIndex;
public int MyIndex
{
get
{
return this._myIndex;
}
set
{
this._myIndex = value;
OnPropertyChanged(nameof(MyIndex));
OnPropertyChanged(nameof(IsLowerThanTheHighestIndex));
}
}
and everywhere you modify MyList
also raise the change, e.g.:
this.MyList.Add(...);
OnPropertyChanged(nameof(IsLowerThanTheHighestIndex));
If you are using an ObservableCollection<T>
you can subscribe to the CollectionChanged
event and call OnPropertyChanged(nameof(IsLowerThanTheHighestIndex));
in the event callback (remember to unsubscribe the event at the end)
HIH
answered Nov 22 at 16:08
fmaccaroni
2,2701824
2,2701824
I had such a solution which worked, but I was curious if there was a way to do it without putting extra code in my ViewModel
– Dennis van Opstal
Nov 23 at 7:27
The other way could be to make a custom control in each platform (or use some sort of custom carrousel) that handles the logic of when the buttons are available and you just bind the command of the buttons
– fmaccaroni
Nov 23 at 13:08
add a comment |
I had such a solution which worked, but I was curious if there was a way to do it without putting extra code in my ViewModel
– Dennis van Opstal
Nov 23 at 7:27
The other way could be to make a custom control in each platform (or use some sort of custom carrousel) that handles the logic of when the buttons are available and you just bind the command of the buttons
– fmaccaroni
Nov 23 at 13:08
I had such a solution which worked, but I was curious if there was a way to do it without putting extra code in my ViewModel
– Dennis van Opstal
Nov 23 at 7:27
I had such a solution which worked, but I was curious if there was a way to do it without putting extra code in my ViewModel
– Dennis van Opstal
Nov 23 at 7:27
The other way could be to make a custom control in each platform (or use some sort of custom carrousel) that handles the logic of when the buttons are available and you just bind the command of the buttons
– fmaccaroni
Nov 23 at 13:08
The other way could be to make a custom control in each platform (or use some sort of custom carrousel) that handles the logic of when the buttons are available and you just bind the command of the buttons
– fmaccaroni
Nov 23 at 13:08
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53433783%2fxamarin-forms-binding-2-properties-to-another-one%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Please share your code and image of your View you want to achieve. It sounds like Next Previous buttons like in Carousel view?
– Nirmal Subedi
Nov 22 at 15:56
@NirmalSubedi, I've posted an image of what I hope to accomplish and it's indeed a kind of carousel. I'm not sure what code to post though.
– Dennis van Opstal
Nov 23 at 7:34