Skip to content

Instantly share code, notes, and snippets.

@JohanLarsson
Created May 10, 2013 08:48
Show Gist options
  • Save JohanLarsson/5553261 to your computer and use it in GitHub Desktop.
Save JohanLarsson/5553261 to your computer and use it in GitHub Desktop.
public class DummyVm : INotifyPropertyChanged
{
public DummyVm()
{
this.Items= new ObservableCollection<DummyItem>(new List<DummyItem>
{
new DummyItem(){Name = "Maverick", Country = "England"},
new DummyItem(){Name = "Johan", Country = "SwedenLand"},
new DummyItem(){Name = "Gregory", Country = "England"},
new DummyItem(){Name = "Jackomel", Country = "Unknown"}
});
}
public int CurrentIndex
{
get { return this._currentIndex; }
set
{
if (value == this._currentIndex) return;
this._currentIndex = value;
this.OnPropertyChanged();
this.OnPropertyChanged("Current");
}
}
public DummyItem Current { get { return this.Items[this.CurrentIndex]; } }
public ObservableCollection<DummyItem> Items { get; set; }
private int _currentIndex;
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
var handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
}
public class DummyItem
{
public string Name { get; set; }
public string Country { get; set; }
}
<Window x:Class="WpfTextboxDummy.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:wpfTextboxDummy="clr-namespace:WpfTextboxDummy"
xmlns:system="clr-namespace:System;assembly=mscorlib"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DataContext="{d:DesignInstance wpfTextboxDummy:DummyVm, IsDesignTimeCreatable=True}"
Title="MainWindow" Height="350" Width="525">
<DockPanel>
<DataGrid ItemsSource="{Binding Items}" SelectedIndex="{Binding CurrentIndex}" DockPanel.Dock="Top"></DataGrid>
<GroupBox Header="DetailView" DockPanel.Dock="Top">
<StackPanel>
<TextBox Text="{Binding Current.Name}"></TextBox>
<TextBox Text="{Binding Current.Country}"></TextBox>
</StackPanel>
</GroupBox>
<StackPanel Orientation="Horizontal">
<Button Click="BackClick">Back</Button>
<TextBox Text="{Binding CurrentIndex}"></TextBox>
<Button Click="ForwardClick">Next</Button>
</StackPanel>
</DockPanel>
</Window>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = new DummyVm();
}
private void BackClick(object sender, RoutedEventArgs e)
{
var vm = (DummyVm)this.DataContext;
if (vm.CurrentIndex > 0)
vm.CurrentIndex--;
}
private void ForwardClick(object sender, RoutedEventArgs e)
{
var vm = (DummyVm)this.DataContext;
if (vm.CurrentIndex < (vm.Items.Count()-1))
vm.CurrentIndex++;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment