向转换器发送 2 个控制值
我的应用程序中有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不使用 ValueConverter,而是使用 ViewModel 中的几个属性。
在每个属性的设置器中,适当更新 TimeSpan。
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.