Products Technologies Demo Docs Blog Support Company

Converting Values when Binding Data to the InputFormat Class

In WPF, you can use a Converter to convert between types such as a String and a Color. But a converter can be also used to convert between different units - for example twips and points. Consider a combo box that should be bound to the InputFormat.FontSize that gets or sets the font's size, in twips, at the current input position. But typically, the font size is selected in points. In TX Text Control .NET for WPF, the MeasureConverter can be utilized which, provides a way to convert a…

Converting Values when Binding Data to the InputFormat Class

In WPF, you can use a Converter to convert between types such as a String and a Color. But a converter can be also used to convert between different units - for example twips and points.

Consider a combo box that should be bound to the InputFormat.FontSize that gets or sets the font's size, in twips, at the current input position. But typically, the font size is selected in points. In TX Text Control .NET for WPF, the MeasureConverter can be utilized which, provides a way to convert a measurement in twips (twentieth of a point) to a value in another unit. The XAML would look like this:

<Window.Resources>
    <tx:MeasureConverter Unit="Point" Decimals="1" x:Key="twipsconv" />
</Window.Resources>

<TextBox
   Height="21"
   Name="textBoxFontSize"
   Text="{Binding ElementName=textControl1,
      Path=InputFormat.FontSize,
      Converter={StaticResource twipsconv},
      Mode=TwoWay}"/>

Unfortunately, this concept is not provided when building .NET applications for Windows Forms. The data binding concept is quite similar, but events are required to convert between units. The following code shows how to add the Binding to the combo box:

Binding b = new Binding(
        "Text",
        textControl1.InputFormat,
        "FontSize",
        true,
        DataSourceUpdateMode.OnPropertyChanged);

b.Format += new ConvertEventHandler(b_Format);
b.Parse += new ConvertEventHandler(b_Parse);

comboBox1.DataBindings.Add(b);

Using the attached events, the units can be converted. A twip is a 1/20 of a point. Accordingly, we just need to multiply or divide the value by 20:

void b_Parse(object sender, ConvertEventArgs e)
{
    if (e.Value == null) return;

    e.Value = Convert.ToInt32(e.Value) * 20;
}
void b_Format(object sender, ConvertEventArgs e)
{
    if (e.Value == null) return;

    e.Value = ((int)e.Value / 20).ToString();
}

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

Related Posts

Windows FormsWPF.NET

Create a Table of Contents in Windows Forms using C#

This article explains how to create a table of contents in Windows Forms using the ribbon or programmatically. Creating a table of contents is required to organize large documents.


ASP.NETWindows FormsWPF

Official TX Text Control .NET Sample Applications Are Now Hosted on GitHub

This article gives a quick overview of the new repositories, their structure and our plans for the future.


ASP.NETJavaScriptDocument Editor

Detect Toggle Button Changes Using a MutationObserver

This article shows how to detect changes of toggle buttons in the ribbon of the web editor using a MutationObserver. The state of a toggle button in the ribbon visualizes the state of a certain…


Windows FormsList.NET

Two Ways to Restart Numbered Lists in TX Text Control

In TX Text Control, numbered lists are continued by default and need to be reset when required. There is more than one way if you want to restart numbered lists in a document. In this article, two…


Windows FormsSampleShortcuts

Zoom Tricks: Disabling CTRL + MOUSE WHEEL and More

This article shows how to disable CTRL + MOUSE WHEEL, implement zooming with keyboard and reset the zoom factor to its default value.