0
votes
<toolkit:DoubleUpDown  Minimum="{Binding Path=ManageScalingFactorsCollection.MinFactor}" 
                                 Maximum="{Binding Path=ManageScalingFactorsCollection.MaxFactor}" 
                                 Value="{Binding Path=ManageScalingFactorsCollection.FactorForSingle.MinFactor, 
                                 UpdateSourceTrigger=PropertyChanged,  Delay={StaticResource DelayExecuteMiliseconds} }"  
                                 Increment="{Binding Path=ManageScalingFactorsCollection.MinStep}" Margin="0,0,5,0" Width="70"
                                 HorizontalContentAlignment="Left"/>

I would like to change my code to bind DoubleUpDown to a list or double[] or DoubleCollection. The problem is that i have a collection of doubles which is predefined and the step/increment isn't constant.

for example {0.1, 0.2, 0.4, 0.5, 0.6, 0.89,1.005} Is there a way to do so ?

1

1 Answers

1
votes

You need to inherit your own class and override some methods, like this:

using System.Linq;
using System.Windows;
using System.Windows.Media;
using Xceed.Wpf.Toolkit;

namespace WpfApplication13
{
class RestrictedDoubleUpDown: DoubleUpDown
{
    public DoubleCollection Range
    {
        get { return (DoubleCollection)GetValue(RangeProperty); }
        set { SetValue(RangeProperty, value); }
    }

    // Using a DependencyProperty as the backing store for Range.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty RangeProperty =
        DependencyProperty.Register("Range", typeof(DoubleCollection), typeof(RestrictedDoubleUpDown),
            new FrameworkPropertyMetadata(new DoubleCollection(), OnRangeChanged));

    private static void OnRangeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var self = (RestrictedDoubleUpDown)d;
        if(self.Range!=null)
            self.Value = self.Range.FirstOrDefault();
    }

    private int currentIndex = 0;

    protected override double IncrementValue(double value, double increment)
    {
        if (currentIndex < Range.Count - 1)
            currentIndex++;
        return Range[currentIndex];
    }

    protected override double DecrementValue(double value, double increment)
    {
        if (currentIndex > 0)
            currentIndex--;
        return Range[currentIndex];
    }


}

}

XAML:

 <Grid>
    <local:RestrictedDoubleUpDown Width="100" Height="23"
                                  Range="{Binding Path=Range, RelativeSource=  {RelativeSource AncestorType=Window}}">

    </local:RestrictedDoubleUpDown>
</Grid>

Code behind:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        Range = new DoubleCollection(new double[] { 0.1, 0.2, 0.4, 0.5, 0.6, 0.89, 1.005 });
        InitializeComponent();
    }

    public DoubleCollection Range { get; set; }
}

Note that I may have forgotten something, but at first glance, it works well.