Hello,
Welcome to our Microsoft Q&A platform!
To set binding for a property, it requires to specify the source. Please set BindingContext
for the page, it should be the page class itemself in above code.
public NotebooksVM()
{
...
BindingContext = this;
}
It's recommend to use a model class to create the properties for data binding. Here is the related code, you could refer to.
public class TestModel : INotifyPropertyChanged
{
bool testValue;
public TestModel(bool testValue)
{
TestCommand = new Command(testMethod);
this.testValue = testValue;
}
public ICommand TestCommand { get; set; }
private void testMethod(object obj)
{
if (testValue)
{
IconSource = "one";
}
else
{
IconSource = "grid_";
}
}
private string iconSource = "grid_";
public string IconSource
{
get
{
return iconSource;
}
set
{
if (iconSource != value)
{
iconSource = value;
NotifyPropertyChanged();
}
}
}
protected virtual void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public event PropertyChangedEventHandler PropertyChanged;
}
public partial class TestPage : ContentPage
{
TestModel model;
public TestPage()
{
InitializeComponent();
model = new TestModel();
BindingContext = model;
}
}
Best Regards,
Jarvan Zhang
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
I will Update my VM, so you can see
I am using this [AddINotifyPropertyChangedInterface]
so I dont have to implement the interface evretime
It seems you don't set binding for the Glyph property. Try to change the value of the Glyph property in code behind directly. You could set bindingContext in page.xaml.cs to implement the command.
@Eduardo Gomez
May I know if you have got any chance to check my reply? Please feel free to share more detailed information to us and it will be very helpful for us to better work out this issue.
Yes but you're using code-behind, I am using a VM and trying to bind the Glyph
How do use the 'LayoutBase' in the page.xaml? It doesn't support to detect the control's type in xaml directly.
like this
hi is my code
https://github.com/eduardoagr/safe.Xamarin
Hi, EduardoGomez-1870. Since you'are using
x:Static
instead of binding to set the value, the value cannot be changed in viewModel. To do this, please use binding instead, you could create a list in viewmodel and add the strings of the IconFonts class.Hello I changed to binding and I don't see the Icon
If I click on the toolbar item, I can see in my console when I change the value
Try setting binding for the IconImageSource property instead. You could use a converter to specify the related properties.
Check the code:
Custom Converter class
Thank you, it works, but when II navigate, I don't see the item
But if I start clicking I can see it
I had to change it
It seems that you didn't set the initial value for the 'Glyph' property of the ViewModel class. Please add the value in the ViewModel class.
Hello, I did, but now when I clicked the first
time is duplicate
114636-second-time-grind.png
Now when I click on the list, I see the list icon again
I did that but I receive it
duplicate icons
I tested the your sample and add the initial value for the 'Glyph' property. It works fine, here are the screenshot of the results.
NotesPage.xaml
NotesVM class
Yes I know its working fine, but the problem is that because I want the initial value to be a list, if you Tap again yo will se the list again. You have to click it three times, and the you will get the expected behavior.
The expected behavior that I want is:
I have a List of Notebooks right? so is logical that I have a List icon
if I change the layout, the grid will appear
The code that we have, works like this
The initial value of the icon is a list, if your clicked it, in doesn't chance until, you clicked again
Is like is being duplicated
Please test the above code, the icon will be changed once you click it.
I tested the code and since the initial value is the list-icon, this is duplicated
Demo
https://1drv.ms/v/s!AuPWj2VdUNuhv-FRigtNuJoY_Pv-MA?e=bxxcvb
If I understand your code correctly, you want to consume the linearLayout with 'Grid' icon and gridLayout with 'FormatListBulleted' icon. But in NotesVM's constructor method, you use linearLayout with 'FormatListBulleted' icon together which is the third case and it'll cause the 'duplicated'. Please change the code in NotesVM's constructor as below.
This is the problem I am facing
https://1drv.ms/u/s!AuPWj2VdUNuhv-FSoDXdz2p3_YUVcw?e=ZiIdyu
As you can see, the initial value is a list, and this is what I want, but when you click it is still a list, so you have to clicked it again in order to change
This is because when first clicking the icon, the Glyph is set to 'FormatListBulleted'.
For this, please set the LayoutBase to GridLayout instead.
haha sorry thanks
Sign in to comment
0 additional answers
Sort by: Most helpful
Activity