Skip to main content

WPF Layout Techniques - Grids

අපි දන්නවා WPF එහෙම නැත්නම් Windows Presentation Foundation කිව්වම Windows applications ගොඩක් ලස්සනට හදාගන්න Microsoft එකෙන් අපිට දීල තියෙන technology එකක්. ඉතින් අපි අද බලමු ඕනෙම application එකක් හදද්දී අපිට ගොඩක් වැදගත් දෙයක් වෙන control layout එක ලේසියෙන් කරගන්නේ කොහොමද කියල. මේකට අපි පාවිච්චි කරන්නේ Grid කියන control එක. අපි අද අපේ උදාහරණය විදිහට ගන්නේ සරල Login form එකක්. කට්ටියට මතක නේ අපි web pages හදද්දී <table></table> tags වලින් layout හදනවා මේ වැඩෙත් ටිකක් විතර එකට සමානයි. වැඩි කතා නැතුව බලමුකෝ අපි මොකද්ද කරන්න හදන්නේ කියලා.

Login Window
දැන් අපි බලමු මේක හදාගන්නේ කොහොමද කියල.
  1. අපි මුලින්ම කරගන්න ඕනෙ වැඩේ තමයි අපේ basic layout එක grid එකක් පාවිච්චි කරලා හදාගන්න එක. මේ තියෙන්නේ ඒකට ඕනෙ කරන xaml code එක.
     <Window x:Class="WpfBasics.MainWindow"  
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
         Title="Login to WPF Sample" Height="200" Width="350" WindowStartupLocation="CenterScreen" WindowStyle="ToolWindow" Background="WhiteSmoke">  
       <Grid>  
         <Grid.RowDefinitions>  
           <RowDefinition Height="35" />  
           <RowDefinition Height="35" />  
           <RowDefinition Height="35" />  
           <RowDefinition Height="35" />  
           <RowDefinition Height="*" />  
         </Grid.RowDefinitions>  
         <Grid.ColumnDefinitions>  
           <ColumnDefinition Width="120" />  
           <ColumnDefinition Width="*" />  
         </Grid.ColumnDefinitions>  
       </Grid>  
     </Window>  
    
    මේකේ සමහර Height Width values * කියල දාල තියෙන්නේ, ඉතිරි වෙන ඉඩ ඔක්කොම ගන්න කියන එක. මේ වැඩේ කරලා ඉවර උනාම ඔයාලගේ window එක මෙන්න මේ වගේ කොටස් කීපයකට බෙදිලා තියෙන්න ඕනෙ. මේකේ තියෙන මේ එක එක cells ඇතුලට තමා අපි අපේ Labels Textboxes වගේ items දාන්නේ.

  2. දැන් අපි ලෑස්ති වෙන්නේ අපේ Heading text එක එහෙමත් නැත්නම් මේ Welcome to WPF Samples කියල ලොකුවට තියෙන text එක දාගන්න.
     <Label Grid.Row="0" Grid.ColumnSpan="2" Margin="20,0" Content="Welcome to WPF Samples" FontSize="16" FontWeight="Bold" />  
    
    දැන් මෙතන ඔයාලට පේනවා Margin, Content වගේ සාමාන්‍යයෙන් අපි පාවිච්චි කරන properties වලට අමතරව Grid.Row="0" Grid.ColumnSpan="2" කියල properties දෙකක් පාවිච්චි වෙනවා. ඒ දෙකෙන් තමා කියවෙන්නේ අපි මේ දාන item එක grid එකේ position වෙන්න ඕනෙ කොහොමද කියල. Grid.Row="0" කිව්වම මේක වැටෙන්නේ grid එකේ පළවෙනි row එකට. Grid.ColumnSpan="2" කිව්වම මේ item එක columns දෙකක් හරහා පැතිරෙනවා කියන එක කියවෙනවා.
  3. දැන් බලමු Username, Password labels එක්ක boxes දාගන්න විදිහ.
    <Label Grid.Row="1" Grid.Column="0" Margin="20,0" Content="Username :" />  
    <TextBox Grid.Row="1" Grid.Column="1" Height="23" HorizontalAlignment="Left" Margin="10,5,0,0" Name="txtUsername" VerticalAlignment="Top" Width="150" />  
    <Label Grid.Row="2" Grid.Column="0" Margin="20,0" Content="Password :" />  
    <PasswordBox Grid.Row="2" Grid.Column="1" Height="23" HorizontalAlignment="Left" Margin="10,5,0,0" Name="pwdPassword" VerticalAlignment="Top" Width="150" />  
    
    Username label එක ගත්තොත් ඒක අපි දාල තියෙන්නේ දෙවැනි row එකේ පළවෙනි column එකට. ඒ වගේ තමා අපි අනිත් controls ටිකත් arrange කරගෙන තියෙන්නේ.
  4. ඊට පස්සේ අපි මේ පහල පෙන්නලා තියෙන විදිහට තමා buttons දෙක දාගන්නේ.
    <StackPanel Grid.Row="3" Grid.Column="1" Orientation="Horizontal">  
           <Button Content="Login" Margin="10,0,5,0" Height="23" Name="btnLogin" Width="75" />  
           <Button Content="Cancel" Height="23" Name="btnCancel" Width="75" />  
    </StackPanel>  
    
    මෙතන මේ තියන StackPanel එකෙන් කරන්නේ ඒකට දාන controls තිරස් අතට හරි සිරස් අතට හරි arrange කරගන්න එක. මේකේ Orientation="Horizontal" කියල තියෙන නිසා එයා අපි දාන controls තිරස් අතට arrange කරගන්නවා.
  5. මේ window එක design කරගන්න ඕනෙ වෙන සම්පූර්ණ xaml code එක තමා පහල තියෙන්නේ.
     <Window x:Class="WpfBasics.MainWindow"  
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
         Title="Login to WPF Sample" Height="200" Width="350" WindowStartupLocation="CenterScreen" WindowStyle="ToolWindow" Background="WhiteSmoke">  
       <Grid>  
         <Grid.RowDefinitions>  
           <RowDefinition Height="35" />  
           <RowDefinition Height="35" />  
           <RowDefinition Height="35" />  
           <RowDefinition Height="35" />  
           <RowDefinition Height="*" />  
         </Grid.RowDefinitions>  
         <Grid.ColumnDefinitions>  
           <ColumnDefinition Width="120" />  
           <ColumnDefinition Width="*" />  
         </Grid.ColumnDefinitions>  
         <Label Grid.Row="0" Grid.ColumnSpan="2" Margin="20,0" Content="Welcome to WPF Samples" FontSize="16" FontWeight="Bold" />  
         <Label Grid.Row="1" Grid.Column="0" Margin="20,0" Content="Username :" />  
         <TextBox Grid.Row="1" Grid.Column="1" Height="23" HorizontalAlignment="Left" Margin="10,5,0,0" Name="txtUsername" VerticalAlignment="Top" Width="150" />  
         <Label Grid.Row="2" Grid.Column="0" Margin="20,0" Content="Password :" />  
         <PasswordBox Grid.Row="2" Grid.Column="1" Height="23" HorizontalAlignment="Left" Margin="10,5,0,0" Name="pwdPassword" VerticalAlignment="Top" Width="150" />  
         <StackPanel Grid.Row="3" Grid.Column="1" Orientation="Horizontal">  
           <Button Content="Login" Margin="10,0,5,0" Height="23" Name="btnLogin" Width="75" />  
           <Button Content="Cancel" Height="23" Name="btnCancel" Width="75" />  
         </StackPanel>  
       </Grid>  
     </Window>  
    
එහෙම නම් ඉතින් කට්ටිය මේ වැඩේ try කරලා බලන්න හරිම ලේසියි, windows forms වලට වඩා ලේසි කියල හිතෙනවා මට නම්.

Comments

Popular posts from this blog

HTML INPUT enhancement with Regular Expressions and Java Scripts

අද කථා කරන්න යන්නේ බොහොම සරල දෙයක් ගැන. සරල උනාට ඉතින් මට මේක කරගන්න ඕන වෙලා හොයන කොට නම් ඉතින් හොයාගන්න ටිකක් කරදර උනා, ඒ නිසාම තමා මම හිතුවේ මේක ගැන ලියන්න ඕනෙ කියල. අපි හැමෝම HTML forms validate කරන්න Java Script පාවිච්චි කරනවා නේ, එක අලුත් දෙයක් නෙමේ. ඒ වගේම තමා Regular Expressions කියන්නෙත් අපිට අලුත් දෙයක් නෙමේ. අපි බලමු මේ දෙකම පාවිච්චි කරලා HTML textbox එකකට අපිට ඕනෙ characters විතරක් enter කරන්න දෙන්නේ කොහොමද කියල. මෙන්න මේක තමා අපි පාවිච්චි කරන HTML page එක. <!DOCTYPE html> <html> <head> <title>JS and RegEx</title> </head> <body> <label for="txtUsername">Username</lable> <input type="text" id="txtUsername" placeholder="Enter Username"/> </body> </html> මේ තියෙන page එක html විදිහට save කරලා browser එකේ බැලුවම මෙන්න මේ වගේ තමා පෙන්නේ. දැන් හිතන්න අපිට මේ username එකට @,#,%,$ වගේ...

How to disable ASP.NET page validations

ASP.NET වල page validation පාවිච්චි වෙන්නේ script injection attacks නවත්ත ගන්න, ඒ ගැන වැඩි විස්තර ඕනෙ නම් මෙතනින් බලාගන්න පුළුවන්. හැබැයි මේක අපිට කරදරයක් වෙන වෙලාවලුත් තියෙනවා. උදාහරණයක් විදියට අපි හිතුවොත් අපි අපේ user ට හිතලම ඉඩ දෙන්න ඕනේ script වගේ දේවල් page එකකට submit කරන්න. මේක උනේ මම වැඩ කරපු Admin site එකක, මේ site ඒක run වෙන්නේ company එකක intranet එකේ, මේක පාවිච්චි කරන්නේ Admin ල විතරයි ඒක අපි වෙන විදියකට authenticate කරනවා ඒ ගැන පස්සේ කියන්නම්. ඉතින් ප්‍රශ්නේ උනේ මේ page validations නිසා අපිට ඕනේ කරන user inputs ගන්න විදියක් නැති උනා. ඒක විසඳගන්න මට සිද්ද උනා page validations disable කරන්න. ඒක කරන්න ක්‍රම දෙකක් තියෙනවා. මොන දේ කරන්නත් ඉස්සෙල්ල site එකේ web.config එක පහල තියෙන විදිහට වෙනස් කරන්න ඕනේ. (සැ. යු. ASP.NET 4.0 web sites සඳහා). <configuration> <system.web> <httpRuntime requestValidationMode="2.0"/> </system.web> </configuration> ඊට පස්සේ පහල තියෙන ක්‍රම දෙකෙන් එකකට වැඩේ කරන්න පුළුවන්. Site එක...

Simple Data Binding in WPF

අපි කලින් ලියුමක WPF වල layouts ලේසියෙන් හදාගන්නේ කොහොමද කියල බැලුවා කට්ටියට මතක ඇති නේද. ඉතින් අද අපි බලන්න යන්නේ මෙතනින් තව පොඩ්ඩක් ඉස්සරහට යන්නේ කොහොමද කියල. ඒ කියන්න අද අපි බලමු අපි මේ හදාගත්ත client window එක කොහොමද අපේ business layer එකත් එක්ක connect කරන්නේ කියල. ඒකට තියෙන සරලම සහ හොඳම විදිහ තමා මේ Data Binding කියල කියන්නේ. Data Binding වලින් අපිට පුළුවන් අපේ Business Layer එකයි UI එකයි අතරේ data auto update කරන්න. Data Binding කොටස් දෙකක් තියෙනවා Unidirectional කියන්නේ source එකේ ඉඳල target එකට හරි target එකේ ඉඳල source එකට හරි එක පැත්තකට විතරක් data update වෙන එකට. Bidirectional කියන්නේ source එකෙන් target එකටයි target එකෙන් source එකයි දෙපැත්තටම data update වෙන එකට. ඒ වගේම binding target එක හැම වෙලේම DependencyProperty එකක් වෙන්න ඕනෙ.(DependencyProperty එකක් කියන්නේ සරලව ගත්තොත් object එකක අපිට runtime එකේදී වෙනස් කරන්න පුළුවන් property එකක්.) ඉස්සෙල්ලම අපි මෙන්න මේ පහල තියෙන Window එක හදාගෙන ඉමු. මේකට ඕනෙ කරන XAML markup එක තමා පහල තියෙන්නේ. <Windo...