Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Custom Rule Engine in WPF - MVVM

, 11 Sep 2010
Custom Rule Engine approach which can evaluate complex combinations of business rules. A sample application is provided in WPF following the MVVM pattern.
<Window x:Class="CustomRulesMVVM.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:vm="clr-namespace:CustomRulesMVVM.ViewModels"
        Title="Custom Rule Engine" Height="377" Width="553">   
    <Grid>               
        <TabControl Height="300" HorizontalAlignment="Left" Margin="22,12,0,0" Name="tabControl1" VerticalAlignment="Top" Width="480">
            <TabItem Header="Country">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition/>
                        <RowDefinition/>
                        <RowDefinition/>
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition/>
                        <ColumnDefinition/>
                        <ColumnDefinition/>
                    </Grid.ColumnDefinitions>
                    <Grid.DataContext>
                        <vm:CountryViewModel/>
                    </Grid.DataContext>
                    <TextBlock Height="23" HorizontalAlignment="Center" Text="Enter Country Name:" VerticalAlignment="Center" />
                    <Button Content="Check Country" Grid.Row="1" Height="23" HorizontalAlignment="Center" VerticalAlignment="Center" Width="100" Command="{Binding SearchCountry}"/>                    
                    <TextBox Grid.Column="1" Height="23" HorizontalAlignment="Center" VerticalAlignment="Center"  Width="120" Text="{Binding MyCountry}" />
                    <TextBlock Grid.Column="1" Grid.Row="1" Height="23" HorizontalAlignment="Center" Foreground="Blue" Text="{Binding SearchCountryResult}" VerticalAlignment="Center"/>
                </Grid>
            </TabItem>
            <TabItem Header="City and Temperature">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition/>
                        <RowDefinition/>
                        <RowDefinition/>
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition/>
                        <ColumnDefinition/>
                        <ColumnDefinition/>
                    </Grid.ColumnDefinitions>
                    <Grid.DataContext>
                        <vm:CityViewModel/>
                    </Grid.DataContext>
                    <TextBlock Height="23" HorizontalAlignment="Center" Text="Enter City Name:" VerticalAlignment="Center" />
                    <TextBox Grid.Column="1" Height="23" HorizontalAlignment="Center" VerticalAlignment="Center"  Width="120" Text="{Binding CityName}" />
                    <TextBlock Height="23" HorizontalAlignment="Center" Grid.Row="1" Text="Enter Temperature:" VerticalAlignment="Center" />
                    <TextBox Grid.Row="1" Grid.Column="1" Height="23" HorizontalAlignment="Center" VerticalAlignment="Center"  Width="120" Text="{Binding Temperature}" />
                    <Button Content="Check City and Temperature" Grid.Row="2" Grid.ColumnSpan="2" Height="23" HorizontalAlignment="Center" VerticalAlignment="Center" Width="200" Command="{Binding SearchCity}"/>
                    <TextBlock Grid.Column="2" Grid.Row="2" Height="46" Width="150" TextWrapping="Wrap" Foreground="Blue" HorizontalAlignment="Center" Text="{Binding SearchCityResult}" VerticalAlignment="Center"/>
                </Grid>
            </TabItem>
            <TabItem Header="City,State and Temperatures">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition/>
                        <RowDefinition/>
                        <RowDefinition/>
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="1*"/>
                        <ColumnDefinition Width="1*"/>
                        <ColumnDefinition Width="1.4*"/>
                        <ColumnDefinition Width="0.6*"/>
                    </Grid.ColumnDefinitions>
                    <Grid.DataContext>
                        <vm:CityStateViewModel/>
                    </Grid.DataContext>
                    <TextBlock Height="23" HorizontalAlignment="Center" Text="Enter City Name:" VerticalAlignment="Center" />
                    <TextBox Grid.Column="1" Height="23" HorizontalAlignment="Center" VerticalAlignment="Center"  Width="100" Text="{Binding CityName}" />
                    <TextBlock Height="23" HorizontalAlignment="Center" Grid.Column="2" Text="Enter City Temperature:" VerticalAlignment="Center"/>
                    <TextBox Grid.Column="3" Height="23" HorizontalAlignment="Center" VerticalAlignment="Center"  Width="50" Text="{Binding CityTemperature}" />
                    <TextBlock Height="23" HorizontalAlignment="Center" Grid.Row="1" Text="Enter State Name:" VerticalAlignment="Center" />
                    <TextBox Grid.Column="1" Grid.Row="1" Height="23" HorizontalAlignment="Center" VerticalAlignment="Center"  Width="100" Text="{Binding StateName}" />
                    <TextBlock Height="23" HorizontalAlignment="Center" Grid.Column="2" Grid.Row="1" Text="Enter State Avg Temperature:" VerticalAlignment="Center"/>
                    <TextBox Grid.Column="3" Grid.Row="1" Height="23" HorizontalAlignment="Center" VerticalAlignment="Center"  Width="50" Text="{Binding StateTemperature}" />
                    <Button Content="Check City and State Temparatures" Grid.Row="2" Grid.ColumnSpan="2" Height="23" HorizontalAlignment="Center" VerticalAlignment="Center" Width="200" Command="{Binding SearchCityState}" />
                    <TextBlock Grid.Column="2" Grid.Row="2" Grid.ColumnSpan="2" Height="46" Width="200" Foreground="Blue" HorizontalAlignment="Center" Text="{Binding SearchCityStateResult}" VerticalAlignment="Center" TextWrapping="Wrap"/>
                </Grid>
            </TabItem>
        </TabControl>
    </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)

Share

About the Author

Renil Joy, Bangalore
Software Developer (Senior) Societe Generale
India India
I am a .Net developer, currently working with Societe Generale Global Solution Centre, Bangalore and was previously with Cognizant.I have more than 8 years of .Net experience in BFSI domain. I am an experienced developer in C#, VB.Net, Silverlight, WPF, WCF, LINQ, Entity Framework, SSIS, NHibernate, ASP.Net and SQL Server.
Follow on   LinkedIn

| Advertise | Privacy | Mobile
Web03 | 2.8.140821.2 | Last Updated 12 Sep 2010
Article Copyright 2010 by Renil Joy, Bangalore
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid