向转换器发送 2 个控制值

发布于 2024-11-16 10:45:57 字数 1661 浏览 2 评论 0原文

我的应用程序中有 2 个整数上下控件。 1 表示时间跨度中的分钟,另一个表示时间跨度中的秒。我想将这些值绑定到时间跨度。我知道我需要为此使用转换器。但是,我还需要在发生任何更改事件时将这两个值发送到转换器。因此,如果用户更改分钟,我需要根据分钟和秒创建一个新的时间跨度。有没有办法将这两个值发送到转换器,或者我是否需要在后面的代码中执行此操作?

这是 2 个控件的 XAML。

<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>

这是转换器代码

[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;
    }
}

我可以让它接受数组或列表吗?如果是这样,如何在 xaml 中完成此操作?

请帮忙!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冰之心 2024-11-23 10:45:57

不使用 ValueConverter,而是使用 ViewModel 中的几个属性。

在每个属性的设置器中,适当更新 TimeSpan。

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");
  }
}

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");
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文