Using the Strikethrough font on a WPF TextBlock

I had a list of tasks displayed in a StackPanel. Each row of tasks had a CheckBox and a TextBlock, the latter being the name of the task. When the user clicked the CheckBox, I wanted the task name to have a strikethrough font to signify that the task was done.

First, my XAML:

        <ItemsControl Canvas.Top="150" ItemsSource="{Binding}" Name="TaskList">
             <ItemsControl.ItemTemplate>
                 <DataTemplate>
                     <StackPanel Orientation="Horizontal">
                         <CheckBox Name="chkFinished" Checked="chkFinished_Clicked" IsChecked="{Binding Finished}" />
                         <TextBlock x:Name="lblTaskName" Text="{Binding TaskName}" />
                     </StackPanel>
                 </DataTemplate>
             </ItemsControl.ItemTemplate>
         </ItemsControl>

For semi-completeness, the data source that the ItemsControl is bound to is an ObservableCollection of a custom class called TaskList:

    public class TaskList
     {         
        public bool Finished { get; set; }
         public string TaskName { get; set; }
         public bool Recurring { get; set; }
        public TaskList(string taskName, bool recurring)
         {
             Finished = false;
             TaskName = taskName;
             Recurring = recurring;
         }
     }

The following few lines are in the Loaded event for the XAML Window control:

private void Window_Loaded(object sender, RoutedEventArgs e) {
 // Declaration. Code to pull records from the DB are not shown.
 ObservableCollection<TaskList> myList = new ObservableCollection<TaskList>() { };
// Bind the list 
ItemList.ItemsSource = myList; }

When the user clicks the Finished CheckBox, the  chkFinished_Clicked fires. The method looks for a matching record among the records bound, and when it finds one, it adds a TextDecoration item to the collection of TextDecorations associated with the TextBlock:

        protected void chkFinished_Clicked(object sender, EventArgs e)
         {
             StackPanel panel = (StackPanel)((CheckBox)sender).Parent;
                         // If any of the CheckBoxes are checked, change the font to strikethrough
             for (int i = 0; i < panel.Children.Count; i++)
             {
                 if (panel.Children[i].GetType().ToString() == "System.Windows.Controls.TextBlock")
                 {
                     ((TextBlock)panel.Children[i]).TextDecorations.Add(TextDecorations.Strikethrough);
                 }
             }
         }
Advertisements
  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: