This is a migrated thread and some comments may be shown as answers.

Solution: RadNumericUpDown respects the RangeAttribute through the RadNumericUpDownAttributeBehavior

0 Answers 17 Views
PropertyGrid
This is a migrated thread and some comments may be shown as answers.
Art
Top achievements
Rank 1
Art asked on 19 Feb 2017, 08:31 PM

Usage:

[System.ComponentModel.DataAnnotations.Range(0, 2)]
//[Display(...)]
public int UpperLabelColumn
{
    get { return _upperLabelColumn; }
    set { Set(() => UpperLabelColumn, ref _upperLabelColumn, value); }
}

In XAML:

<DataTemplate >
    <t:RadNumericUpDown t:AutoBindBehavior.UpdateBindingOnElementLoaded="Value" IsInteger="True">
        <i:Interaction.Behaviors>
            <bs:RadNumericUpDownAttributeBehavior />
        </i:Interaction.Behaviors>
    </t:RadNumericUpDown>
</DataTemplate>

Implementation:

public class RadNumericUpDownAttributeBehavior : Behavior<RadNumericUpDown>
{
    protected override void OnAttached()
    {
        AssociatedObject.Loaded += OnLoaded;
        AssociatedObject.DataContextChanged += OnDataContextChanged;
    }
 
    protected override void OnDetaching()
    {
        AssociatedObject.Loaded -= OnLoaded;
        AssociatedObject.DataContextChanged -= OnDataContextChanged;
    }
 
    private void OnLoaded(object sender, RoutedEventArgs e)
    {
        UpdateBinding((RadNumericUpDown) sender);
    }
 
    private void OnDataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
    {
        UpdateBinding((RadNumericUpDown)sender);
    }
 
    private void UpdateBinding(RadNumericUpDown sender)
    {
        var binding = sender.GetBindingExpression(RadRangeBase.ValueProperty);
 
        if (binding == null)
            return;
 
        var attributes = GetCustomAttributes(binding.DataItem, binding.ParentBinding.Path.Path);
 
        if (attributes == null || attributes.Length == 0)
            return;
 
        var rangeAttribute = attributes.OfType<RangeAttribute>().FirstOrDefault();
 
        if (rangeAttribute == null)
            return;
 
        sender.Minimum = Convert.ToDouble(rangeAttribute.Minimum);
        sender.Maximum = Convert.ToDouble(rangeAttribute.Maximum);
    }
 
    protected static object[] GetCustomAttributes(object source, string propertyPath)
    {
        if (source == null || string.IsNullOrEmpty(propertyPath))
            return null;
 
        PropertyInfo propertyInfo = null;
 
        var currentType = source.GetType();
        var path = propertyPath.Split('.');
 
        for (int i = 0, count = path.Length; i < count; i++)
        {
            var propertyStep = path[i];
            propertyInfo = currentType.GetProperty(propertyStep);
 
            if (propertyInfo == null || source == null || i == count - 1)
                break;
 
            source = propertyInfo.GetValue(source, null);
            currentType = propertyInfo.PropertyType;
        }
 
        return propertyInfo != null ? propertyInfo.GetCustomAttributes(true) : null;
    }
}

 

Microsoft Silverlight (64-bit) Version: 5.1.41105.0
Windows 8.1 (64-bit)
Internet Explorer 11.0.9600.17416
Telerik 2017.1.117.1050

 

No answers yet. Maybe you can help?

Tags
PropertyGrid
Asked by
Art
Top achievements
Rank 1
Share this question
or