Quantcast
Channel: Extended WPF Toolkit™ Community Edition
Viewing all articles
Browse latest Browse all 4964

New Post: Unit conversion on CollectionControl (propertygrid)

$
0
0
Hi,

You can define a custom editor for your Length property and add an IValueConverter to convert from Cm to inches.
Here's an example.
<xctk:CollectionControl x:Name="_collectionControl" />
 public enum UnitTypeEnum
  {
    Cm,
    Inch
  }

  public partial class MainWindow : Window
  {
    public MainWindow()
    {
      InitializeComponent();

      _collectionControl.ItemsSource = new ObservableCollection<MyData>()
      {
        new MyData() { Name = "Tom", Length = 52.5, UnitType = UnitTypeEnum.Cm },
        new MyData() { Name = "Julia", Length = 46.9, UnitType = UnitTypeEnum.Cm },
        new MyData() { Name = "Mike", Length = 101.0, UnitType = UnitTypeEnum.Cm },
        new MyData() { Name = "Nancy", Length = 75.5, UnitType = UnitTypeEnum.Cm },
      };
    }
  }

  public class MyData
  {
    [PropertyOrder(1)]
    public string Name
    {
      get;
      set;
    }

    [PropertyOrder( 3 )]
    [DependsOn( "UnitType" )]
    [Editor( typeof( LengthEditor ), typeof( LengthEditor ) )]
    public double Length
    {
      get;
      set;
    }

    [PropertyOrder( 2 )]
    public UnitTypeEnum UnitType
    {
      get;
      set;
    }

    public override string ToString()
    {
      return this.Name;
    }
  }

  public class LengthEditor : Xceed.Wpf.Toolkit.PropertyGrid.Editors.ITypeEditor
  {
    public FrameworkElement ResolveEditor( Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem propertyItem )
    {
      var upDown = new PropertyGridEditorDoubleUpDown();

      var unitType = ( ( MyData )propertyItem.Instance ).UnitType;

      //create the binding from the bound property item to the editor
      var _binding = new Binding( "Value" ); //bind to the Value property of the PropertyItem
      _binding.Source = propertyItem;
      _binding.Mode = propertyItem.IsReadOnly ? BindingMode.OneWay : BindingMode.TwoWay;
      _binding.Converter = (unitType == UnitTypeEnum.Inch) ? new CmToInchConverter() : null;
      BindingOperations.SetBinding( upDown, PropertyGridEditorDoubleUpDown.ValueProperty, _binding );

      return upDown;
    }
  }

  public class CmToInchConverter : IValueConverter
  {
    public object Convert( object value, Type targetType, object parameter, CultureInfo culture )
    {
      return ( ( double )value ) / 2.54d;
    }

    public object ConvertBack( object value, Type targetType, object parameter, CultureInfo culture )
    {
      throw new NotImplementedException();
    }
  }
Please note that the "DependsOn" attribute is only available in the Plus Edition of the Toolkit. It will call the ResolveEditor() method on your custom editor when the "depends on" property is modified, to refresh it.

――――
Get more controls, features, updates and technical support with Xceed Toolkit Plus for WPF

Viewing all articles
Browse latest Browse all 4964

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>