Tuesday, January 6, 2015

Coding Experiences Part 2

Points to be covered:


  • 1. Create a user control to simplify WPF forms.
  • 2. Use Strategy pattern in a complex decision making scenario.
  • 3. Converters (this post)
  • 4. Create a template to make form creations faster
  • 5. Create a common print template to print all forms in WPF



Use datatemplates to switch views based on conditions

In your view place this in Resources tag


 <ControlTemplate x:Key="XYZ">  
 <local:XYZDetailsView DataContext="{Binding SelectedViewItem}"/>  
 </ControlTemplate>  

Then put a datatrigger in a Control style. When the bound property from the view model matches with the value, the static resource defined in your resources will define your control's template.


 <Grid>  
 <Control>  
  <Control.Style>  
   <Style>  
    <Style.Triggers>  
      <DataTrigger Binding="{Binding Path=SelectedViewItem.Type}" Value="XYZ">  
       <Setter Property="Control.Template" Value="{StaticResource XYZ}" />  
      </DataTrigger>  
    </Style.Triggers>  
   </Style>  
  </Control.Style>  
 </Control>  
 </Grid>  

Multi Datatriggers OR conditon example

Following example demonstrates an OR condition used in a multi data trigger

<TextBlock>
   <TextBlock.Style>  
      <Style TargetType="TextBlock" BasedOn="{StaticResource dd}">  
        <Setter Property="Text" Value="" />  
        <Style.Triggers>  
           <MultiDataTrigger>  
             <MultiDataTrigger.Conditions>  
                <Condition Binding="{Binding EmpInformation.EmpType}" Value="PE" />  
             </MultiDataTrigger.Conditions>  
             <Setter Property="Text" Value="Permenant Employee" />  
           </MultiDataTrigger>  
           <MultiDataTrigger>  
             <MultiDataTrigger.Conditions>  
                <Condition Binding="{Binding EmpInformation.EmpType}" Value="Permenant Employee" />  
             </MultiDataTrigger.Conditions>  
             <Setter Property="Text" Value="Permenant Employee" />  
           </MultiDataTrigger>  
        </Style.Triggers>  
      </Style>  
   </TextBlock.Style>  
 </TextBlock>  

No comments :