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:



Example Control



This is the control I'm trying to build.










share|improve this question
























  • 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















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:



Example Control



This is the control I'm trying to build.










share|improve this question
























  • 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













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:



Example Control



This is the control I'm trying to build.










share|improve this question















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:



Example Control



This is the control I'm trying to build.







c# xamarin data-binding xamarin.forms






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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


















  • 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












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






share|improve this answer





















  • 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











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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

























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






share|improve this answer





















  • 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















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






share|improve this answer





















  • 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













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






share|improve this answer












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







share|improve this answer












share|improve this answer



share|improve this answer










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


















  • 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


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

What visual should I use to simply compare current year value vs last year in Power BI desktop

How to ignore python UserWarning in pytest?

Alexandru Averescu