how to databind a ListView programmatically and update it with PageFunction

I’ve read a lot of tutorials about databinding in WPF but often it’s all about XAML. In my TwitterViewer application in WPF I need to update the TwitterManager class with username and password of the user. I choose to set the binding in code. Here is the constructor of the main page :

public MainPage() { InitializeComponent(); //create a provider odp = new ObjectDataProvider(); odp.MethodName = “GetStatuses”; odp.ObjectInstance = new TwitterManager(“tewmgd”, “xxxx”); odp.IsInitialLoadEnabled = false; Binding myBinding = new Binding(); myBinding.Source = odp; //set the source of the ItemsSource statusList.SetBinding(ListView.ItemsSourceProperty, myBinding); odp.Refresh(); }

With an  hyperLink the user can navigate to another page to change the settings (username and password). The PageFunction control allows the launching page to subscribe to the Return event and since it’s a generic class, the return object is typesafe :

void settingsLink_Click(object sender, RoutedEventArgs e) { //create a new instance of the settings page SettingsPage pageFunction = new SettingsPage(); //subscribe to the return event pageFunction.Return += new ReturnEventHandler<TwitterManager>
(OnSettingsReturned); this.NavigationService.Navigate(pageFunction); }

public void OnSettingsReturned(object sender,ReturnEventArgs<TwitterManager> e) { //change the source of the provider odp.ObjectInstance = e.Result; //refresh the provider and with it the listview odp.Refresh(); }

In the target page the button event handler uses the OnReturn method to navigate backward and set the result object :

private void OnClickDone(object sender, RoutedEventArgs e) { OnReturn(new ReturnEventArgs<TwitterManager>( new TwitterManager(this.txtUserName.Text,this.txtPassword.Text))); }

Update, screenshoots of the two pages :

Twitter Viewer
Twitter Viewer
Twitter Viewer (2)
Twitter Viewer (2)

billet publié dans les rubriques coding le