Click here to Skip to main content
15,885,309 members
Articles / Desktop Programming / WPF

AutoPropertyChanged - Type-safe INotifyPropertyChanged Implementation

Rate me:
Please Sign up or sign in to vote.
3.54/5 (11 votes)
24 May 2010CPOL6 min read 35K   227   14  
Typesafe INotifyPropertyChanged implementation without run-time Reflection and without lambda expressions.
<Window x:Class="AutoPropertyChangedDemo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:vm="clr-namespace:AutoPropertyChangedDemo.ViewModel"
        xmlns:vw="clr-namespace:AutoPropertyChangedDemo.Views"
        Title="AutoPropertyChangedDemo" Height="350" Width="640"
        Background="{StaticResource WindowBrush}">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="175*" />
            <ColumnDefinition Width="300" />
            <ColumnDefinition Width="175*" />
        </Grid.ColumnDefinitions>
        <vw:LeftView Grid.Column="0"/>
        <ScrollViewer Grid.Column="1" VerticalScrollBarVisibility="Auto">
            <StackPanel Margin="10,0,10,0">
                <TextBlock FontSize="13" FontWeight="DemiBold" Margin="0,0,0,5">
                    A few words about this application
                </TextBlock>
                <TextBlock TextWrapping="Wrap" Margin="0,0,0,5">
                    This demo binds two Views to one ViewModel.
                </TextBlock>
                <TextBlock TextWrapping="Wrap" Margin="0,0,0,5">
                    The view model itself is derived from ImplementsPropertychanged.<LineBreak />
                    This is not a must since the ViewModel may expose an inner property to the XAML databinding, which itself can be derived from ImplementsPropertyChanged.
                </TextBlock>
                <TextBlock TextWrapping="Wrap" Margin="0,0,0,5">
                    The ListView is bound to an ObservableCollection (which doesn't need the AutoPropertyNotificationChanged attribute).<LineBreak />
                    The items within the ListView themselves derive again from ImplementsPropertyChanged.<LineBreak />
                    This way every change on the ListViews' items is propagated to the ViewModel and back to the view.
                </TextBlock>
                <TextBlock FontSize="13" FontWeight="DemiBold" Margin="0,0,0,5">
                    Pros and Cons
                </TextBlock>
                <TextBlock TextWrapping="Wrap" Margin="0,0,0,5">
                    + The solution is refactoring-safe: you don't need to duplicate property names as strings.<LineBreak/>
                    + Just one reflection-step is needed, so no run-time reflection overhead introduced.<LineBreak/>
                    + The usage is quite easy<LineBreak/>
                    + Quite low overhead: each property has a dynamic link to its parent container, but the PropertyEventArgs is just initialized once for each property (as a hidden static field).
                </TextBlock>
                <TextBlock TextWrapping="Wrap" Margin="0,0,0,5">
                    - Not-so-nice is the generic Property0...N parameter, but in this context it is unavoidable to allow compile-time distinction between properties having the same types.<LineBreak />
                    - Still more runtime overhead as implementing IPropertyNotificationChanged by foot :-)
                </TextBlock>
            </StackPanel>
        </ScrollViewer>
        <vw:RightView Grid.Column="2"/>
    </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
I am working with computers for quite a long time now, starting with BASIC on an old "Acorn Electron".

Then i continued with C and moved quickly on to C++ with some Java in the mean time.

Now, my personal favorite is C# and i love especially WPF and its versatileness, as well as the TPL. I'm still on a quite steep learning curve, but thanks to this community (among others of course Smile | :) ) i'm doing okay.

Comments and Discussions