Создайте XAML следующим образом:
<Window x:Class="WpfTestApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" >
<Grid>
<TextBlock Name="txtSelectedName" Width="100" Height="30" Margin="113,110,290,171" />
<Button Name="btnPrevious" Width="100" Height="30" Margin="46,159,357,122" Content="Previous" Click="btnPrevious_Click" />
<Button Name="btnNext" Width="100" Height="30" Margin="192,158,211,122" Content="Next" Click="btnNext_Click" />
</Grid>
Если вы хотите простое решение, напишите логику для обновления текстового кода TextBlock в коде позади.
private int _index = 0;
public MainWindow()
{
InitializeComponent();
_clients = GetClients();
txtSelectedName.Text = _clients[_index].Name;
}
private void btnPrevious_Click(object sender, RoutedEventArgs e)
{
_index = _index == 0 ? _clients.Count - 1 : _index - 1;
txtSelectedName.Text = _clients[_index].Name;
}
private void btnNext_Click(object sender, RoutedEventArgs e)
{
_index = _index == _clients.Count - 1 ? 0 : _index + 1;
txtSelectedName.Text = _clients[_index].Name;
}
Но если это серьезное приложение, и со временем область применения становится все более сложной, вы можете использовать шаблон MVVM следующим образом:
<Window x:Class="WpfTestApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" >
<Grid>
<TextBlock Name="txtSelectedName" Width="100" Height="30" Margin="113,110,290,171" Text="{Binding Path=SelectedName}" />
<Button Name="btnPrevious" Width="100" Height="30" Margin="46,159,357,122" Content="Previous" Command="{Binding Path=Next}" />
<Button Name="btnNext" Width="100" Height="30" Margin="192,158,211,122" Content="Next" Command="{Binding Path=Previous}" />
</Grid>
public MainWindow()
{
InitializeComponent();
DataContext = new MainWindowViewModel();
}
public class MainWindowViewModel :INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private List<Client> _clients;
private int _index;
public MainWindowViewModel()
{
_clients = GetClients();
_index = 0;
SelectedName = _clients[_index].Name;
}
protected void OnPropertyChange(string propertyName)
{
if(PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
public List<Client> GetClients()
{
List<Client> clients = new List<Client>();
clients.Add(new Client()
{
Name = "Name1",
});
clients.Add(new Client()
{
Name = "Name2",
});
return clients;
}
private string _selectedName;
public string SelectedName
{
get { return _selectedName; }
set
{
if(_selectedName != value)
{
_selectedName = value;
OnPropertyChange("SelectedName");
}
}
}
private RelayCommand _next;
public RelayCommand Next
{
get
{
return _next ?? (_next = new RelayCommand(param => this.SetNextName()));
}
}
private void SetNextName()
{
_index = _index == _clients.Count - 1 ? 0 : _index + 1;
SelectedName = _clients[_index].Name;
}
private RelayCommand _previous;
public RelayCommand Previous
{
get
{
return _previous ?? (_previous = new RelayCommand(param => this.SetPreviousName()));
}
}
private void SetPreviousName()
{
_index = _index == 0 ? _clients.Count - 1 : _index - 1;
SelectedName = _clients[_index].Name;
}
}
public class RelayCommand : ICommand
{
private Action<object> _execute;
private Predicate<object> _canexecute;
public RelayCommand(Action<object> execute) : this(execute, null){}
public RelayCommand(Action<object> execute, Predicate<object> canExecute)
{
if (execute == null)
throw new ArgumentException("execute");
_execute = execute;
_canexecute = canExecute;
}
public void Execute(object parameter)
{
_execute(parameter);
}
public bool CanExecute(object parameter)
{
return _canexecute == null || _canexecute(parameter);
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value;}
remove { CommandManager.RequerySuggested -= value;}
}
}