Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 91519

Please help me understand what's wrong with this binding

$
0
0

I've read through the binding material on the getting started, and I watched probably a half hour of the university course. I'm trying to get a basic binding to work.

I have this XAML toolbar item

< ContentPage.ToolbarItems>
        < ToolbarItem BindingContext="CodeView" x:Name="CountButton" Text="{Binding CodeCount}" />
    < /ContentPage.ToolbarItems>

I have created a basic class that has a single attribute. (For testing)
`
public class SelectedCodes : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
if (PropertyChanged == null)
return;

        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

    public SelectedCodes ()
    {
    }

    private int _CodeCount = 0;
    public int CodeCount
    {
        get 
        {
            return _CodeCount;              
        }
        set
        { 
            _CodeCount = value;
            OnPropertyChanged ("CodeCount");
        }
    }
}

`

The Class is instantiated on the constructor.
`
//Declared at top of page
public SelectedCodes CodeView { get; set; }

    //In the constructor before InitializeComponent() is called
    CodeView = new SelectedCodes ();

And finally in the code I update the value
CodeView.CodeCount++;
`

As I understand things this should work. The toolbar button never shows though I'm assuming due to the binding, because if I replace {Binding CodeCount} with just a word or number typed into the XAML it shows.


Viewing all articles
Browse latest Browse all 91519

Trending Articles