private static object DisplayTextCoerceValueCallback(DependencyObject d, object baseValue) { if (!(baseValue is string)) { return(baseValue); } TextControl textControl = (TextControl)d; string value = (string)baseValue; if (!textControl.IsHourDisplay || !value.EndsWith("M")) { return(value); } else { string ampm = value.Substring(value.Length - 2); string number = value.Remove(value.Length - 2); TextBlock tb = new TextBlock(); tb.Inlines.Add(number); Run _ampm = new Run(ampm); _ampm.FontSize = 8.5; _ampm.BaselineAlignment = BaselineAlignment.TextTop; tb.Inlines.Add(_ampm); return(tb); } }
private void InitializeClockLabels() { for (int i = 0; i < 24; i++) { RowDefinition row = new RowDefinition(); row.Height = new GridLength(1, GridUnitType.Star); clockTimesGrid.RowDefinitions.Add(row); Border border2 = new Border(); border2.SetResourceReference(Border.BorderBrushProperty, "Gray"); if (i < 23) { border2.BorderThickness = new Thickness(0, 0, 0, 1); } else { // We still want to add the border, otherwise some other // functions which loop through would have to handle for // the border being nonexistent. border2.BorderThickness = new Thickness(0); } Grid.SetRow(border2, i); clockTimesGrid.Children.Add(border2); TextControl text = new TextControl(); text.FontSize = 15; text.Name = "h" + i.ToString(); text.IsHourDisplay = true; Grid.SetRow(text, i); clockTimesGrid.Children.Add(text); } UpdateTimeFormat(); }
private void UpdateTimeFormat() { try { bool twelvehour = Settings.TimeFormat == TimeFormat.Standard; bool amTaken = false; bool pmTaken = false; Rect thisBounds = new Rect(0, 0, scrollViewer.ActualWidth, scrollViewer.ActualHeight); for (int i = 0; i < 24; i++) { TextControl text = clockTimesGrid.Children[i * 2 + 1] as TextControl; // Give a +1px vertical tolerance to handle for layout rounding. Rect textBounds = new Rect(text.TranslatePoint(new Point(0, 1), scrollViewer), text.TranslatePoint(new Point(text.ActualWidth, text.ActualHeight), scrollViewer)); if (thisBounds.Contains(textBounds)) { if (twelvehour) { if (i < 12) { if (!amTaken) { amTaken = true; text.DisplayText = (i != 0 ? i : 12).ToString() + "AM"; } else { text.DisplayText = (i != 0 ? i : 12).ToString(); } } else if (!pmTaken) { pmTaken = true; text.DisplayText = (i != 12 ? i - 12 : 12).ToString() + "PM"; } else { text.DisplayText = (i != 12 ? i - 12 : 12).ToString(); } } else { text.DisplayText = i.ToString().PadLeft(2, '0'); } } // TODO: This is temporary - once layout is fixed to 22px increments // this will no longer be necessary. else if (thisBounds.IntersectsWith(textBounds)) { if (twelvehour) { if (i < 12) { text.DisplayText = (i != 0 ? i : 12).ToString(); } else { text.DisplayText = (i != 12 ? i - 12 : 12).ToString(); } } else { text.DisplayText = i.ToString().PadLeft(2, '0'); } } } } catch { // VS Designer } }