简体   繁体   中英

Sending 2 controls values to a converter

I have 2 integer up down controls in my application. 1 represents minutes from a time span the other represents seconds from a time span. I want to bind those values to the timespan. I know that I need to use a converter for this. However I also need to send both values to the converter on any change event. So if the user changes the minutes I need to make a new timespan from the minutes and seconds. Is there a way to send both of these values to a converter or do I need to do I need to do this in the code behind?

Here is the XAML for the 2 controls.

<extToolKit:IntegerUpDown Minimum="0" Margin="1,3,0,4" x:Name="iupApproachMin">
    <extToolKit:IntegerUpDown.Value>
        <PriorityBinding FallbackValue="50">
            <Binding Path="VehicleEntryTaskStandards.MaxEntryTimeRequirement" Converter="{StaticResource timeSpanConvertor}">
            </Binding>
        </PriorityBinding>
    </extToolKit:IntegerUpDown.Value>
</extToolKit:IntegerUpDown>
<Label>min</Label>
<extToolKit:IntegerUpDown Minimum="0" Maximum="59" Margin="1,3,0,4" FormatString="00" Value="10"></extToolKit:IntegerUpDown>
<Label>sec</Label>

Here is the converters code

[ValueConversion(typeof(TimeSpan),typeof(int))]
public class TimespanConverter:IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        int minutes = ((TimeSpan)value).Minutes;
        return minutes;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        TimeSpan resultTimeSpan = new TimeSpan();

        int minutes;
        if (int.TryParse(value.ToString(), out minutes))
        {
            resultTimeSpan = new TimeSpan(0, minutes, 0);
            return resultTimeSpan;
        }
        return DependencyProperty.UnsetValue;
    }
}

Can I have it accept an array or list. If so how can this be done in xaml?

Please help!

Instead of using a ValueConverter, use a couple of properties in a ViewModel.

In the setter of each of these properties have the TimeSpan updated appropriately.

private TimeSpan _time;
public TimeSpan Time 
{
  get { return _time; }
  set 
  { 
    _time = value; 
    RaisePropertyChanged("Time");
  }
}

private int _minutes
public int Minutes
{ 
  get { return _minutes; }
  set 
  {
    _minutes = value;
    CalculateTimeSpan();
    RaisePropertyChanged("Minutes");
  }
}

private int _seconds
public int Seconds
{ 
  get { return _seconds; }
  set 
  {
    _seconds= value;
    CalculateTimeSpan();
    RaisePropertyChanged("Seconds");
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM