This sample is just to demonstrate the basic concepts and wiring involved in a simple master/detail scenario, where you want to show an edit form for the currently selected item in a DataGrid.
Just download, unzip, open and run!
This sample shows how little code is needed to build a user interface, and bind to a collection of data.
In this example, the data is created as a collection in code and the DataContext of the page is itself, which exposes the property to direct binding, for the DataGrid ItemsSource.
MyCollection = new List<Person>
{
new Person { FirstName = "Fred", LastName="Smith" },
new Person { FirstName = "Tom", LastName="Jones" }
};
DataContext = this;
MyCollection = new List<Person> { new Person { FirstName = "Fred", LastName="Smith" }, new Person { FirstName = "Tom", LastName="Jones" } }; DataContext = this;
<DataGrid x:Name="dataGrid" HorizontalAlignment="Center" VerticalAlignment="Center" ItemsSource="{Binding MyCollection}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="First Name" Binding="{Binding FirstName}"/>
<DataGridTextColumn Header="Last Name" Binding="{Binding LastName}"/>
</DataGrid.Columns>
</DataGrid>
<DataGrid x:Name="dataGrid" HorizontalAlignment="Center" VerticalAlignment="Center" ItemsSource="{Binding MyCollection}" AutoGenerateColumns="False"> <DataGrid.Columns> <DataGridTextColumn Header="First Name" Binding="{Binding FirstName}"/> <DataGridTextColumn Header="Last Name" Binding="{Binding LastName}"/> </DataGrid.Columns> </DataGrid>
<Grid Margin="10" DataContext="{Binding SelectedItem, ElementName=dataGrid}" >
<Grid Margin="10" DataContext="{Binding SelectedItem, ElementName=dataGrid}" >
<TextBox Grid.Column="1" HorizontalAlignment="Left" TextWrapping="Wrap" Text="{Binding FirstName, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Center" MinWidth="100"/>
<TextBox Grid.Column="1" HorizontalAlignment="Left" TextWrapping="Wrap" Text="{Binding LastName, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Center" Grid.Row="1" MinWidth="100"/>
<TextBox Grid.Column="1" HorizontalAlignment="Left" TextWrapping="Wrap" Text="{Binding FirstName, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Center" MinWidth="100"/> <TextBox Grid.Column="1" HorizontalAlignment="Left" TextWrapping="Wrap" Text="{Binding LastName, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Center" Grid.Row="1" MinWidth="100"/>
This project was created as demonstration to answer to a forum question.
