Tuesday, November 6, 2007

Using Visual brush and Value converter in wpf

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows;
using System.Text.RegularExpressions;
using System.Windows.Controls;
using System.Windows.Shapes;
namespace CardAccess.Mapping.Components.APBAreas
{
class ApbAreaHelper
{
#region givingtext
public static VisualBrush TextBrush(FrameworkElement element, string text, Brush background)
{
Border border = new Border();
border.Background = Brushes.Transparent;
border.BorderThickness = new Thickness(1);
border.BorderBrush = Brushes.Transparent;
Binding heightBinding = new Binding("ActualHeight");
heightBinding.Source = element;


Binding widthBinding = new Binding("ActualWidth");
widthBinding.Source = element;
border.SetBinding(Border.WidthProperty, widthBinding);
border.SetBinding(Border.HeightProperty, heightBinding);


TextBlock textBlock = new TextBlock();
textBlock.Padding = new Thickness(2);
textBlock.Text = text;
textBlock.Foreground = Brushes.LightSlateGray;
textBlock.Opacity = .5;
textBlock.HorizontalAlignment = HorizontalAlignment.Center;
textBlock.VerticalAlignment = VerticalAlignment.Center;
Binding FontSizeBinding = new Binding("ActualWidth");
FontSizeBinding.Source = element;
FontSizeBinding.Converter = new ArithmeticConverter();
//if (element is Polygon) FontSizeBinding.ConverterParameter = "*0.07";
// else
FontSizeBinding.ConverterParameter = "*0.1";
textBlock.SetBinding(TextBlock.FontSizeProperty , FontSizeBinding);
// textBlock.SetBinding( TextBlock.WidthProperty , FontSizeBinding);
if (element is Polygon) textBlock.FontSize = 14;


border.Child = textBlock;
VisualBrush brush = new VisualBrush();
brush.Stretch = Stretch.None;
brush.Visual = border;
return brush;
}

#endregion
}
class ArithmeticConverter : IValueConverter
{
private const string ArithmeticParseExpression = "([+\\-*/]{1,1})\\s{0,}(\\-?[\\d\\.]+)";
private Regex arithmeticRegex = new Regex(ArithmeticParseExpression);
#region IValueConverter Members
object IValueConverter.Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value is double && parameter != null)
{
string param = parameter.ToString();
if (param.Length > 0)
{
Match match = arithmeticRegex.Match(param);
if (match != null && match.Groups.Count == 3)
{
string operation = match.Groups[1].Value.Trim();
string numericValue = match.Groups[2].Value;
double number = 0;
if (double.TryParse(numericValue, out number)) // this should always succeed or our regex is broken
{
double valueAsDouble = (double)value;
double returnValue = 0;
switch (operation)
{
case "+":
returnValue = valueAsDouble + number;
break;
case "-":
returnValue = valueAsDouble - number;
break;
case "*":
returnValue = valueAsDouble * number;
break;
case "/":
returnValue = valueAsDouble / number;
break;
}
return returnValue;
}
}
}
}
return null;
}
object IValueConverter.ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new Exception("The method or operation is not implemented.");
}
#endregion
}
}

No comments: