开发者

Sending 2 controls values to a converter

开发者 https://www.devze.com 2023-03-14 08:04 出处:网络
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 t

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;
     开发者_StackOverflow中文版   }
        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");
  }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消