Please help me understand what's wrong with this binding

LukeConnerLukeConner USMember, University ✭✭

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.

Best Answer

Answers

  • LukeConnerLukeConner USMember, University ✭✭
    edited August 2015

    So that worked and it didn't. Making those changes did cause the ToolbarItem to show up, but even though I can see OnPropertyChanged is being called using a breakpoint and see that the value of CodeCount is updated correctly, the text of the label doesn't change.

    I'm having the same problem with my listview. That one I had the bindingcontext on the backend, so I didn't have that string problem. I experience the same thing though where changes to the object that happen on the ItemSelected event are not updating the UI.

  • stvansolanostvansolano UMInsider, University ✭✭✭
    edited August 2015

    CodeView needs to be initialized before setting the BindingContext. Try

    public YourContentPage // Constructor of your ContentPage
    {
        InitializeComponent();
        CodeView = new SelectedCodes();
        this.BindingContext = this;
    }
    

    It would be better if you can attach some code to be more accurate

  • LukeConnerLukeConner USMember, University ✭✭

    I've attached a zip file of the xaml, code behind and the simple view model.

  • LukeConnerLukeConner USMember, University ✭✭

    I really appreciate the help by the way. I've been doing C# for a long time, and actually did a fair bit of Silverlight when it first came out, so most of this stuff has been pretty simple. I don't know why the binding stuff is giving me such a hard time.

  • stvansolanostvansolano UMInsider, University ✭✭✭

    No problem :) Will take a look. Here is a good material that might be helpful to you in the meantime

  • LukeConnerLukeConner USMember, University ✭✭

    If this works better, I made a blank app with only a label and a button that updates that labels text. This way you can open it and run it, but it does exactly what I'm trying to do with my app.

    https://www.dropbox.com/s/ftprl00g3jeb4re/test.zip?dl=0

Sign In or Register to comment.