When i tried MVVM Bindings i get problem when changing selectedIndex. I could send a project solution (i tried attaching but didn't allow me to). Let me know. When you click Next Agent button i don't see anytabs getting selected
ViewModel
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace SilverlightApplication1.ViewModel
{
public class AgentsVM: INotifyPropertyChanged
{
private int index;
public int SelectedIndex
{
get
{
return index;
}
set {
index = value;
RaisePropertyChanged("SelectedIndex");
}
}
public void IncreaseTab()
{
index++;
_agents = null;
RaisePropertyChanged("Agents");
RaisePropertyChanged("SelectedIndex");
}
private ObservableCollection<Agent> _agents;
public ObservableCollection<Agent> Agents
{
get
{
if (_agents == null)
{
_agents = new ObservableCollection<Agent>();
Agent agent1 = new Agent();
agent1.Title = "Agent #1";
agent1.AgentInformation.BirthDate = new DateTime(1975, 01, 20);
agent1.AgentInformation.FirstName = "Mike";
agent1.AgentInformation.LastName = "Henderson";
_agents.Add(agent1);
Agent agent2 = new Agent();
agent2.Title = "Agent #2";
agent2.AgentInformation.BirthDate = new DateTime(1986, 05, 01);
agent2.AgentInformation.FirstName = "John";
agent2.AgentInformation.LastName = "Madox";
_agents.Add(agent2);
Agent agent3 = new Agent();
agent3.Title = "Agent #3";
agent3.AgentInformation.BirthDate = new DateTime(1950, 12, 15);
agent3.AgentInformation.FirstName = "Steve";
agent3.AgentInformation.LastName = "Livingston";
_agents.Add(agent3);
}
return _agents;
}
}
public RelayCommand CmdNextAgent
{
get
{
return new RelayCommand(
() => this.IncreaseTab()
);
}
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
}
public class RelayCommand: ICommand
{
private Action _action;
public RelayCommand(Action action)
{
_action = action;
}
#region ICommand Members
public bool CanExecute(object parameter)
{
return true;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
_action();
}
#endregion
}
}
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace SilverlightApplication1.ViewModel
{
public class AgentsVM: INotifyPropertyChanged
{
private int index;
public int SelectedIndex
{
get
{
return index;
}
set {
index = value;
RaisePropertyChanged("SelectedIndex");
}
}
public void IncreaseTab()
{
index++;
_agents = null;
RaisePropertyChanged("Agents");
RaisePropertyChanged("SelectedIndex");
}
private ObservableCollection<Agent> _agents;
public ObservableCollection<Agent> Agents
{
get
{
if (_agents == null)
{
_agents = new ObservableCollection<Agent>();
Agent agent1 = new Agent();
agent1.Title = "Agent #1";
agent1.AgentInformation.BirthDate = new DateTime(1975, 01, 20);
agent1.AgentInformation.FirstName = "Mike";
agent1.AgentInformation.LastName = "Henderson";
_agents.Add(agent1);
Agent agent2 = new Agent();
agent2.Title = "Agent #2";
agent2.AgentInformation.BirthDate = new DateTime(1986, 05, 01);
agent2.AgentInformation.FirstName = "John";
agent2.AgentInformation.LastName = "Madox";
_agents.Add(agent2);
Agent agent3 = new Agent();
agent3.Title = "Agent #3";
agent3.AgentInformation.BirthDate = new DateTime(1950, 12, 15);
agent3.AgentInformation.FirstName = "Steve";
agent3.AgentInformation.LastName = "Livingston";
_agents.Add(agent3);
}
return _agents;
}
}
public RelayCommand CmdNextAgent
{
get
{
return new RelayCommand(
() => this.IncreaseTab()
);
}
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
}
public class RelayCommand: ICommand
{
private Action _action;
public RelayCommand(Action action)
{
_action = action;
}
#region ICommand Members
public bool CanExecute(object parameter)
{
return true;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
_action();
}
#endregion
}
}
<UserControl x:Class="SilverlightApplication1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:telerik="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls"
xmlns:telerikInput="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Input"
xmlns:telerikNavigation="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Navigation">
<UserControl.Resources>
<DataTemplate x:Key="AgentHeaderTemplate">
<Grid>
<TextBlock Text="{Binding Path=Title}" />
</Grid>
</DataTemplate>
<DataTemplate x:Key="AgentContentTemplate">
<Grid>
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
<TextBlock Text="First Name:" Margin="0 10 0 0" />
<TextBox Text="{Binding Path=AgentInformation.FirstName}" IsReadOnly="True" />
<TextBlock Text="Last Name:" Margin="0 10 0 0" />
<TextBox Text="{Binding Path=AgentInformation.LastName}" IsReadOnly="True" />
<TextBlock Text="Birth Date:" Margin="0 10 0 0" />
<telerikInput:RadCalendar DisplayMode="MonthView"
DisplayDate="{Binding Path=AgentInformation.BirthDate}"
SelectedDate="{Binding Path=AgentInformation.BirthDate}" />
</StackPanel>
</Grid>
</DataTemplate>
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="White">
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
<telerikNavigation:RadTabControl x:Name="tabControl1" Width="500" Height="500"
SelectedIndex="{Binding SelectedIndex}"
ItemsSource="{Binding Agents}"
ItemTemplate="{StaticResource AgentHeaderTemplate}"
ContentTemplate="{StaticResource AgentContentTemplate}" />
<Button Content="Load Data" Click="LoadDataButton_Click" />
<Button Command="{Binding CmdNextAgent}" Content="Next Agent"/>
</StackPanel>
</Grid>
</UserControl>
----------------------------
App.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SilverlightApplication1.ViewModel;
namespace SilverlightApplication1
{
public partial class App : Application
{
public App()
{
this.Startup += this.Application_Startup;
this.Exit += this.Application_Exit;
this.UnhandledException += this.Application_UnhandledException;
InitializeComponent();
}
private void Application_Startup(object sender, StartupEventArgs e)
{
var AgentsVM = new AgentsVM();
var mainPage = new MainPage(){DataContext=AgentsVM};
this.RootVisual = mainPage;
}
private void Application_Exit(object sender, EventArgs e)
{
}
private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
{
// If the app is running outside of the debugger then report the exception using
// the browser's exception mechanism. On IE this will display it a yellow alert
// icon in the status bar and Firefox will display a script error.
if (!System.Diagnostics.Debugger.IsAttached)
{
// NOTE: This will allow the application to continue running after an exception has been thrown
// but not handled.
// For production applications this error handling should be replaced with something that will
// report the error to the website and stop the application.
e.Handled = true;
Deployment.Current.Dispatcher.BeginInvoke(delegate { ReportErrorToDOM(e); });
}
}
private void ReportErrorToDOM(ApplicationUnhandledExceptionEventArgs e)
{
try
{
string errorMsg = e.ExceptionObject.Message + e.ExceptionObject.StackTrace;
errorMsg = errorMsg.Replace('"', '\'').Replace("\r\n", @"\n");
System.Windows.Browser.HtmlPage.Window.Eval("throw new Error(\"Unhandled Error in Silverlight Application " + errorMsg + "\");");
}
catch (Exception)
{
}
}
}
}