Click here to Skip to main content
15,881,248 members
Articles / Desktop Programming / WPF

AutoRefresh Entity Framework Data Using SQL Server Service Broker

Rate me:
Please Sign up or sign in to vote.
4.77/5 (35 votes)
14 Sep 2012CPOL3 min read 167.3K   3.8K   95  
How to use SqlDependency to refresh Entity Framework Object Sets automatically on DB changes
<Window x:Class="MyChat.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:MyChat="clr-namespace:MyChat" Title="My Chat" Height="350" Width="525"
        >

    <Window.DataContext>

        <MyChat:MainViewModel x:Name="viewModel" />

    </Window.DataContext>

    <Grid>

        <Grid.RowDefinitions>

            <RowDefinition Height="200*" />

            <RowDefinition Height="Auto" />

            <RowDefinition Height="63*" />

        </Grid.RowDefinitions>

        <Grid.ColumnDefinitions>

            <ColumnDefinition Width="Auto" />

            <ColumnDefinition Width="268*" />

            <ColumnDefinition Width="117*" />

        </Grid.ColumnDefinitions>
        <TextBlock Grid.Row="1"
                     Name="textBlock1"
                     Text="Name"
                      TextAlignment="Right" />

        <TextBlock Grid.Row="2"
                      Name="textBlock2"
                      Text="Message"
                      TextAlignment="Right" />
        <TextBox Grid.Column="1"
                   Grid.Row="1"
                   Name="textBoxName"
                    Text="{Binding Path=Name}" />
        <TextBox Grid.Column="1"
                    Grid.Row="2"
                    Name="textBoxMessage"
                    Grid.ColumnSpan="2"
                    VerticalScrollBarVisibility="Auto"
                    Text="{Binding Path=Message}"
                    TextWrapping="WrapWithOverflow"
                    AcceptsReturn="True"
                    AcceptsTab="True" />

        <DataGrid Grid.ColumnSpan="3"
                     Name="dataGrid1"
                     AutoGenerateColumns="False"
                     ItemsSource="{Binding Path=Chat}">

            <DataGrid.Columns>

                <DataGridTextColumn Binding="{Binding Path=Date}"
                                       Header="Date" />

                <DataGridTextColumn Binding="{Binding Path=Name}"
                                       Header="Name" />

                <DataGridTextColumn Binding="{Binding Path=Message}"
                                       Header="Message" />

            </DataGrid.Columns>

        </DataGrid>

        <Button Content="Send"
                   Grid.Column="2"
                   Grid.Row="1"
                   Name="buttonSend"
                   Click="buttonSend_Click" />
    </Grid>
</Window>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions