Run Class in Silverlight

Hello All,


While using Silverlight most of you must have come across a situation wherein you have to show the text like this:


This is my text.


One question comes into mind is "How to do this in Silverlight" as we can do this in HTML using span tag.


The Run class which is inherited from the abstract System.Windows.Documents.Inline.


Run Class has following Properties:
  1. FontFamily
  2. FontSize
  3. FontStyle
  4. FontWeight
  5. TextDecorations
and some more.


In Xaml code it was very easy to do it.


Example:


<TextBlock x:Name="tb">
     <Run FontWeight="Bold">This</Run>
     <Run Foreground="Blue">is my</Run>
     <Run ForeStyle="Italic">Text.</Run>
</TextBlock>


Output:


This is my Text.


Similarly, we can do it in code behind:


tb.Inlines.Add(new Run() { Text="This ", FontSize=12, FontWeight = FontWeights.Bold });
tb.Inlines.Add(new Run() { Text="is my ", FontSize=12, Foreground = new SolidColorBrush(Colors.Blue});
tb.Inlines.Add(new Run() { Text="Text.", FontSize=12, FontStyle = FontStyles.Italic });


and thats all you have to do.


Happy coding :)

Silverlight Datagrid with SelectAll CheckBox

     Yesterday one of my colleagues asked me about the way to get select all functionality in datagrid. I referred him to a very good article by Lee Corner.

     But before he worked on it I thought of implementing it on my own. The functionality is to have all checkbox checked if header checkbox is checked and if any one row of datagrid checkbox is unchecked then header checkbox must be unchecked.

     When I checked with Lee’s article I found that if you unchecked any row of datagrid then header checkbox must be unchecked and that was not there in that article. So, I did some search on google and found the solution for it.
Here is the xaml code:



<data:DataGrid x:Name=″myDataGrid″ AutoGenerateColumns=″False″>
        <data:DataGrid.Columns>
             <data:DataGridTemplateColumn>
                   <data:DataGridTemplateColumn.HeaderStyle>
                        <Style TargetType=″dataprimitives:DataGridColumnHeader″>
                             <Setter Property="Template">
                                  <Setter.Value>
                                       <ControlTemplate>
                                            <Grid>
                                                 <Grid.ColumnDefinitions>
                                                      <ColumnDefinition> </ColumnDefinition>
                                                 </Grid.ColumnDefinitions>
                                                <CheckBox x:Name="chkAll" Click="chkAll_Click" Grid.Column="0"/>
                                           </Grid>
                                      </ControlTemplate>
                                 </Setter.Value>
                            </Setter>
                       </Style>
                  </data:DataGridTemplateColumn.HeaderStyle>
                  <data:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                              <CheckBox x:Name="chkBoxID" Click="chkBoxID_Click" Tag="{Binding Path=ID}" />
                        </DataTemplate>
                 </data:DataGridTemplateColumn.CellTemplate>
          </data:DataGridTemplateColumn>
      <data:DataGridTextColumn Header="First Name" Width="150" Binding="{Binding Path=FirstName}" FontSize="10" />
       <data:DataGridTextColumn Header="Last Name" Width="150" Binding="{Binding Path=LastName}" FontSize="10" />
   </data:DataGrid.Columns>
</data:DataGrid>


In Code Behind:
I’m using employee class to get ID, First Name and Last Name.



public class Employee
{
         public int ID { get; set; }
         public string FirstName { get; set; }
         public string LastName { get; set; }
         public Employee(int id, String firstName, String lastName)
         {
                this.ID = id;
                this.FirstName = firstName;
                this.LastName = lastName;
         }
         public static List GetSampleEmployeeList()
         {
                return new List(new Employee[4] {
                                     new Employee(1, "Lalit", "Sharma"),
                                     new Employee(2,"Abhijit", "Rajhans"),
                                     new Employee(3,"Nikhil", "Pai"),
                                     new Employee(4,"Asif", "Shaikh")
                   });
         }
}


List employee = null;
List checkboxes = new List();


public DatagridCheckbox()
{
         InitializeComponent();
         employee = Employee.GetSampleEmployeeList();
         myDataGrid.ItemsSource = employee;
         myDataGrid.LoadingRow += new EventHandler(myDataGrid_LoadingRow);
}
void myDataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
{
         CheckBox chk = myDataGrid.Columns[0].GetCellContent(e.Row) as CheckBox;
         chk.IsChecked = false;
         checkboxes.Add(chk);
}


private void chkAll_Click(object sender, RoutedEventArgs e)
{
         CheckBox chk = sender as CheckBox;
         bool check = chk.IsChecked.Value;
         foreach (CheckBox chkbox in checkboxes)
              chkbox.IsChecked = check;
}


private void chkBoxID_Click(object sender, RoutedEventArgs e)
{
         CheckBox cb = GetCheckBoxWithParent(this.myDataGrid, typeof(CheckBox), "chkAll");
         if (cb != null)
         {

               cb.IsChecked = false;
         }
}


private CheckBox GetCheckBoxWithParent(UIElement parent, Type targetType, string CheckBoxName)
{
         if (parent.GetType() == targetType && ((CheckBox)parent).Name == CheckBoxName)
         {
                  return (CheckBox)parent;
         }
         CheckBox result = null;
         int count = VisualTreeHelper.GetChildrenCount(parent);
         for (int i = 0; i < count; i++)
         {
              UIElement child = (UIElement)VisualTreeHelper.GetChild(parent, i);
              if (GetCheckBoxWithParent(child, targetType, CheckBoxName) != null)
              {
                    result = GetCheckBoxWithParent(child, targetType, CheckBoxName);
                    break;
              }
       }
       return result;
}


Happy Coding :)




Note: I apologize for any mistakes in advance as this being my first post.


Comments are always welcome.