Listing Value Converter Converting Spending to Bar Width

using System; using System.Windows; using System.Windows.Data; using System.Windows.Shapes;

namespace Ch04_DataBinding.Recipe4_4 {

public class SpendingToBarWidthConverter : IValueConverter {

public object Convert(object value, Type targetType, object parameter, System.Globalization.Culturelnfo culture)

//verify validity of all the parameters if (value.GetTypeQ != typeof(double) || targetType != typeof(double) | parameter == null

| parameter.GetTypeQ != typeof(SpendingCollection)) return null; //cast appropriately double Spending = (double)value;

double Total = ((SpendingCollection)parameter).Total; //find the xAxis

Rectangle rectXAxis = (Rectangle)((Page)Application.Current.RootVisual)

.FindName("rectXAxis"); //calculate bar width in proportion to the xAxis width return (Spending / Total) * rectXAxis.Width;

public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.Culturelnfo culture)

//verify validity of all the parameters if (value.GetTypeQ != typeof(double) || targetType != typeof(double) | parameter == null

| parameter.GetType() != typeof(SpendingCollection)) return null; //cast appropriately double BarWidth = (double)value;

double Total = ((SpendingCollection)parameter).Total; //find the xAxis

Rectangle rectXAxis = (Rectangle)((Page)Application.Current.RootVisual)

.FindName("rectXAxis"); //calculate new spending keeping total spending constant based on //new bar width to xAxis width ratio return (BarWidth / rectXAxis.Width) * Total;

To convert the spending value into bar width in SpendingToBarWidthConverter .Convert(), we calculate the ratio of the spending value in question to the total spending evaluated from the SpendingCollection passed in as parameter. We then calculate the bar width as the same ratio applied to the total width of the X axis of the graph, also defined as a Rectangle named rectXAxis in XAML. In SpendingToBarWidthConverter .ConvertBack() we reverse that calculation.

Listing 4-17 shows the SpendingToPercentageStringConverter code. The calculation of the percentage value in Convert() is again based off the spending total derived from the SpendingCollection instance, and then formatted appropriately to a string. Since we never do the reverse conversion, we do not implement ConvertBack() in this case.

Listing 4-17. Value Converter Converting Spending to a Percentage String using System;

using System.Windows.Data;

namespace Ch04_DataBinding.Recipe4_4 {

public class SpendingToPercentageStringConverter : IValueConverter {

public object Convert(object value, Type targetType, object parameter, System.Globalization.Culturelnfo culture)

//verify validity of all the parameters if (value.GetType() != typeof(double) || targetType != typeof(string) I parameter == null

I parameter.GetType() != typeof(SpendingCollection)) return null; //cast appropriately double Spending = (double)value;

double Total = ((SpendingCollection)parameter).Total; //calculate the spending percentage and format as string return ((Spending / Total) * 100).ToString("###.##") + 11 %";

public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.Culturelnfo culture)

throw new NotImplementedException();

■ Note There is no requirement that a value converter also perform a type conversion. In the code sample for SpendingToBarWidthConverter for example, we convert values of the same data type double, where the conversion is one of context—that is, from one kind of measure (Spending) to another (Width). Therefore, it is called a value conversion. There is another concept known as a TypeConverter, which we will discuss in more detail in Chapter 5.

Listing 4-18 shows the code-behind for the Page. Of note is the MouseMove handler Rectangle_MouseMove() for each Rectangle representing a bar in the ItemsControl. In the handler we calculate the distance moved as the difference of the current mouse position and its previous position, and change the Width of the bar accordingly. We then store the current position as the previous position for the next move.

0 0

Post a comment

  • Receive news updates via email from this site