Xamarin Forms: Binding 2 properties to another one
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 |
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 '18 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 '18 at 7:34
add a comment |
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 '18 at 16:08
Dennis van Opstal
asked Nov 22 '18 at 15:08
Dennis van OpstalDennis van Opstal
779829
779829
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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 at 7:34
add a comment |
1 Answer
1
active
oldest
votes
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 '18 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 '18 at 13:08
add a comment |
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',
autoActivateHeartbeat: false,
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
});
}
});
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
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 '18 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 '18 at 13:08
add a comment |
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 '18 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 '18 at 13:08
add a comment |
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 '18 at 16:08
fmaccaronifmaccaroni
2,3801824
2,3801824
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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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.
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 '18 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 '18 at 7:34