Share via


Sample XAML File: WPF Databinding and Entity Framework 4.1

The following XAML code is used in the Tutorial: Creating a Conceptual Model from an Existing Database using Tools (Entity Framework 4.1) walkthrough.

<Window x:Class="SchoolModelWPFTest.MainWindow"
        xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="450" Width="800" Loaded="Window_Loaded" Closing="Window_Closing">
    <!-- The code begind code sets layoutGrid to the root of the object graph.-->
    <Grid Name="layoutGrid">
        <Grid.RowDefinitions>
            <RowDefinition Height="25" />
            <RowDefinition Height="25" />
            <RowDefinition Height="*"  />
            <RowDefinition Height="25" />
            <RowDefinition Height="*" />
            <RowDefinition Height="30" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="100" />
        </Grid.ColumnDefinitions>
        <WrapPanel Grid.Row="0" Grid.ColumnSpan="2" Margin="0,0,0,0">
                <Label Height="38" Width="150" Name="departmentLabel" VerticalAlignment="Top" 
               HorizontalAlignment="Left" >Department:            </Label>
            <!-- comboBoxDepartment points to the root of the graph, that is why the Path is not specified-->
            <ComboBox DisplayMemberPath="Name" ItemsSource="{Binding}"
                  IsSynchronizedWithCurrentItem="true" 
                  Width="200" Height="30" Name="comboBoxDepartment" 
                  VerticalAlignment="Top" SelectionChanged="comboBoxDepartment_SelectionChanged" />
        </WrapPanel>
        <Label Grid.Row="1">Online Course: </Label>
        <!-- listViewOnline Path is set programmatically inside of the comboBoxDepartment_SelectionChanged event.-->
        <ListView Name="listViewOnline" ItemsSource="{Binding}" Grid.Row="2" Grid.ColumnSpan="2" 
                  Margin="0,5,0,15" >
            <ListView.View>
                <GridView AllowsColumnReorder="False" ColumnHeaderToolTip="OnlineCourses" >
                    <GridViewColumn DisplayMemberBinding="{Binding Path=CourseID}" 
                        Header="CourseID" Width="70"/>
                    <!--The TextBox controls are embedded in the two of the following columns.
                        This is done to enable editing in the ListView control. -->
                    <GridViewColumn Header="Title" Width="100">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBox Height="25" Width="100" Text="{Binding Path=Title}" />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn Header="Credits" Width="100" >
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBox Height="25" Width="100" Text="{Binding Path=Credits}" />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn Header="URL" Width="300" >
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBox Height="25" Width="300" Text="{Binding Path=URL}" />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>

        <WrapPanel HorizontalAlignment="Center" Grid.Row="2" Grid.Column="3" Margin="0,40,0,0" Orientation="Vertical">
            <Button Content="Add New" Height="23" HorizontalAlignment="Left" Margin="0,0,0,0" Name="buttonAddOnline" 
                    VerticalAlignment="Top" Width="75" Click="buttonAddOnline_Click" />
            <Button Content="Delete" Height="23" HorizontalAlignment="Left" Margin="0,0,0,0" Name="buttonDeleteOnline" 
                    VerticalAlignment="Top" Width="75" Click="buttonDeleteOnline_Click" />
        </WrapPanel>
        <Label Grid.Row="3">Onsite Course:</Label>
        <!-- listViewOnsite Path is set programmatically inside of the comboBoxDepartment_SelectionChanged event.-->
        <ListView ItemsSource="{Binding}" Name="listViewOnsite" Grid.Row="4" Grid.ColumnSpan="2" 
                  Margin="0,5,0,5">
            <ListView.View>
                <GridView AllowsColumnReorder="False" ColumnHeaderToolTip="OnsiteCourses">
                    <GridViewColumn DisplayMemberBinding="{Binding Path=CourseID}" Header="CourseID" Width="70" />
                    <GridViewColumn Header="Title" Width="100">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBox Height="25" Text="{Binding Path=Title}" Width="100" />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn Header="Credits" Width="100">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBox Height="25" Text="{Binding Path=Credits}" Width="100" />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn Header="Location" Width="100">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBox Height="25" Text="{Binding Path=Location}" Width="100" />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn Header="Days" Width="100">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBox Height="25" Text="{Binding Path=Days}" Width="100" />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn Header="Time" Width="100">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBox Height="25" Text="{Binding Path=Time}" Width="100" />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>

        <WrapPanel HorizontalAlignment="Center" Grid.Row="4" Grid.Column="2" 
                   Margin="11,40,13,0" Orientation="Vertical">
            <Button Content="Add New" Height="23" HorizontalAlignment="Left" Margin="0,0,0,0" Name="buttonAddOnsite" 
                VerticalAlignment="Top" Width="75" Click="buttonAddOnsite_Click" />
            <Button Content="Delete" Height="23" HorizontalAlignment="Right" Margin="0,0,0,0" Name="buttonDeleteOnsite" 
                VerticalAlignment="Top" Width="75" Click="buttonDeleteOnsite_Click" />
        </WrapPanel>

        <WrapPanel HorizontalAlignment="Center" Grid.Row="6" Grid.ColumnSpan="2">
            <Button Content="Save" Name="buttonSave" HorizontalAlignment="Left" Margin="0,0,0,0"
            VerticalAlignment="Bottom" Width="70" Click="buttonSave_Click" />
            <Button Name="buttonClose" HorizontalAlignment="Right" Margin="0,0,0,0" 
            VerticalAlignment="Bottom" Width="70" Click="buttonClose_Click">Close</Button>
        </WrapPanel>
    </Grid>
</Window>