Click here to Skip to main content
15,894,343 members
Articles / Programming Languages / C++

Visual C++ and WinRT/Metro - Databinding Basics

Rate me:
Please Sign up or sign in to vote.
4.85/5 (16 votes)
18 Oct 2011CPOL8 min read 90.1K   495   25  
An introduction to using WinRT databinding with Visual C++
#include "CustomPropertyProviderBase.h"
#include "DelegateCommand.h"
#include "BookmarkViewModel.h"

using namespace Windows::UI::Xaml::Input;
using namespace Windows::Foundation::Collections;
using namespace Windows::Foundation;
using namespace Windows::System;

ref class MainViewModel sealed : public CustomPropertyProviderBase
{
	String^ title;
	String^ editAuthor;
	String^ editTitle;
	String^ editUrl;
	int editId;
	unsigned int editIndex;
	BookmarkModel bookmarkModel;

public:
	virtual void AddCustomProperties() override
	{		
		AddCustomProperty("Title", "String", MAKEPROPGET(MainViewModel, Title), MAKEPROPSET(MainViewModel, Title, String^));
		AddCustomProperty("UpdateTitleCommand", "ICommand", MAKEPROPGET(MainViewModel, UpdateTitleCommand), MAKEPROPSET(MainViewModel, UpdateTitleCommand, ICommand^));
		AddCustomProperty("Bookmarks", "IObservableVector<Object>", MAKEPROPGET(MainViewModel, Bookmarks), nullptr);
	
		AddCustomProperty("OpenCommand", "ICommand", MAKEPROPGET(MainViewModel, OpenCommand), MAKEPROPSET(MainViewModel, OpenCommand, ICommand^));
		AddCustomProperty("EditCommand", "ICommand", MAKEPROPGET(MainViewModel, EditCommand), MAKEPROPSET(MainViewModel, EditCommand, ICommand^));
		AddCustomProperty("DeleteCommand", "ICommand", MAKEPROPGET(MainViewModel, DeleteCommand), MAKEPROPSET(MainViewModel, DeleteCommand, ICommand^));
		AddCustomProperty("SaveCommand", "ICommand", MAKEPROPGET(MainViewModel, SaveCommand), MAKEPROPSET(MainViewModel, SaveCommand, ICommand^));
		AddCustomProperty("AddCommand", "ICommand", MAKEPROPGET(MainViewModel, AddCommand), MAKEPROPSET(MainViewModel, AddCommand, ICommand^));

		AddCustomProperty("ShowListBox", "String", MAKEPROPGET(MainViewModel, ShowListBox), MAKEPROPSET(MainViewModel, ShowListBox, String^));
		AddCustomProperty("ShowEditor", "String", MAKEPROPGET(MainViewModel, ShowEditor), nullptr);

		AddCustomProperty("EditAuthor", "String", MAKEPROPGET(MainViewModel, EditAuthor), MAKEPROPSET(MainViewModel, EditAuthor, String^));
		AddCustomProperty("EditTitle", "String", MAKEPROPGET(MainViewModel, EditTitle), MAKEPROPSET(MainViewModel, EditTitle, String^));
		AddCustomProperty("EditUrl", "String", MAKEPROPGET(MainViewModel, EditUrl), MAKEPROPSET(MainViewModel, EditUrl, String^));

	}

	MainViewModel()
	{
		title = "Bookmarks - A basic Metro app using Visual C++";
		showListBox = Visibility::Visible;
		editId = -1;

		UpdateTitleCommand = ref new DelegateCommand(ref new ExecuteDelegate(this, &MainViewModel::Update));

		OpenCommand = ref new DelegateCommand(ref new ExecuteDelegate(this, &MainViewModel::Open));
		EditCommand = ref new DelegateCommand(ref new ExecuteDelegate(this, &MainViewModel::Edit));
		DeleteCommand = ref new DelegateCommand(ref new ExecuteDelegate(this, &MainViewModel::Delete));
		SaveCommand = ref new DelegateCommand(ref new ExecuteDelegate(this, &MainViewModel::Save));
		AddCommand = ref new DelegateCommand(ref new ExecuteDelegate(this, &MainViewModel::Add));

		Bookmarks = ref new Vector<Object^>();

		LoadData();		
	}

	property String^ Title
	{
		String^ get()
		{
			return title;
		}

		void set(String^ value)
		{
			if(title != value)
			{
				title = value;
				FirePropertyChanged("Title");
			}
		}
	}

	property String^ EditAuthor
	{
		String^ get()
		{
			return editAuthor;
		}

		void set(String^ value)
		{
			if(editAuthor != value)
			{
				editAuthor = value;
				FirePropertyChanged("EditAuthor");
			}
		}
	}
	property String^ EditTitle
	{
		String^ get()
		{
			return editTitle;
		}

		void set(String^ value)
		{
			if(editTitle != value)
			{
				editTitle = value;
				FirePropertyChanged("EditTitle");
			}
		}
	}

	property String^ EditUrl
	{
		String^ get()
		{
			return editUrl;
		}

		void set(String^ value)
		{
			if(editUrl != value)
			{
				editUrl = value;
				FirePropertyChanged("EditUrl");
			}
		}
	}

	Visibility showListBox;

	property String^ ShowListBox
	{
		String^ get()
		{
			return showListBox == Visibility::Collapsed ? "Collapsed" : "Visible";
		}

		void set(String^ value)
		{
			Visibility visibility = value == "Collapsed" ? Visibility::Collapsed : Visibility::Visible;

			if(showListBox != visibility)
			{
				showListBox = visibility;
				FirePropertyChanged("ShowListBox");
				FirePropertyChanged("ShowEditor");
			}
		}
	}

	property String^ ShowEditor
	{
		String^ get()
		{
			return showListBox == Visibility::Collapsed ? "Visible" : "Collapsed";
		}
	}

	void Update(Object^ parameter)
	{
		this->Title = "Did this fire?";
		this->ShowListBox = "Visible";
	}

	property ICommand^ UpdateTitleCommand;

	property ICommand^ OpenCommand;

	void Open(Object^ parameter)
	{
		Launcher::LaunchDefaultProgram(ref new Uri((String^)parameter), LaunchingOptions::None);
	}

	property ICommand^ EditCommand;

	void Edit(Object^ parameter)
	{
		BookmarkViewModel^ bookmarkViewModel = (BookmarkViewModel^)parameter;
		Bookmarks->IndexOf(bookmarkViewModel, editIndex);
		editId = bookmarkViewModel->Id;
		this->EditAuthor = bookmarkViewModel->Author;
		this->EditTitle = bookmarkViewModel->Title;
		this->EditUrl = bookmarkViewModel->Url;		
	
		this->ShowListBox = "Collapsed";
	}

	property ICommand^ DeleteCommand;

	void Delete(Object^ parameter)
	{
		BookmarkViewModel^ bookmarkViewModel = (BookmarkViewModel^)parameter;
		unsigned int index;
		if(Bookmarks->IndexOf(bookmarkViewModel, index))
		{
			try
			{
				Bookmarks->RemoveAt(index);
				bookmarkModel.Delete(bookmarkViewModel->Id);			
			}
			catch(...)
			{
			}
		}
	}

	property ICommand^ AddCommand;

	void Add(Object^ parameter)
	{
		editId = -1;
		this->EditAuthor = "";
		this->EditTitle = "";
		this->EditUrl = "";		
	
		this->ShowListBox = "Collapsed";
	}

	property ICommand^ SaveCommand;

	void Save(Object^ parameter)
	{
		if(editId == -1)
		{
			auto bookmark = bookmarkModel.AddNew(this->EditAuthor->Data(), this->EditTitle->Data(), this->EditUrl->Data());
			Bookmarks->Append(ref new BookmarkViewModel(bookmark));
		}
		else
		{
			BookmarkViewModel^ bookmarkViewModel = (BookmarkViewModel^)Bookmarks->GetAt(editIndex);
			bookmarkViewModel->Author = this->EditAuthor;
			bookmarkViewModel->Title = this->EditTitle;
			bookmarkViewModel->Url = this->EditUrl;				

			bookmarkModel.Update(editId, bookmarkViewModel->Author->Data(), bookmarkViewModel->Title->Data(), bookmarkViewModel->Url->Data());
		}

		editId = -1;

		this->ShowListBox = "Visible";
	}

	property Vector<Object^>^ Bookmarks;

private:
	void LoadData()
	{
		auto items = bookmarkModel.GetBookmarks();
		for(auto it = items.begin(); it != items.end(); it++)
		{
			Bookmarks->Append(ref new BookmarkViewModel(*it));
		}
	}
};

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
Nish Nishant is a technology enthusiast from Columbus, Ohio. He has over 20 years of software industry experience in various roles including Chief Technology Officer, Senior Solution Architect, Lead Software Architect, Principal Software Engineer, and Engineering/Architecture Team Leader. Nish is a 14-time recipient of the Microsoft Visual C++ MVP Award.

Nish authored C++/CLI in Action for Manning Publications in 2005, and co-authored Extending MFC Applications with the .NET Framework for Addison Wesley in 2003. In addition, he has over 140 published technology articles on CodeProject.com and another 250+ blog articles on his WordPress blog. Nish is experienced in technology leadership, solution architecture, software architecture, cloud development (AWS and Azure), REST services, software engineering best practices, CI/CD, mentoring, and directing all stages of software development.

Nish's Technology Blog : voidnish.wordpress.com

Comments and Discussions