在您的 Grid 中将转换器定义为 StaticResource,然后将文本框与 Converter 和 Converter Parameter 一起绑定到持续时间。 p>
<UserControl
x:Class="Test_Timer.Timer"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Test_Timer"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Name="timer">
<Grid>
<Grid.Resources>
<local:DateFormatter x:Key="DurationConverter" />
</Grid.Resources>
<StackPanel Orientation="Horizontal>
<TextBox x:Name="hoursBox" Text="{Binding vm.Duration, Mode=TwoWay,
Converter={StaticResource DurationConverter},
ConverterParameter=hoursBox}"/>
<TextBlock Text=":" />
<TextBox x:Name="minutesBox" Text="{Binding vm.Duration, Mode=TwoWay,
Converter={StaticResource DurationConverter},
ConverterParameter=minutesBox}"/>
<TextBlock Text=":" />
<TextBox x:Name="secondsBox" Text="{Binding vm.Duration, Mode=TwoWay,
Converter={StaticResource DurationConverter},
ConverterParameter=secondsBox}"/>
</StackPanel>
</Grid
</UserControl>
然后您需要在命名空间 Test_Timer 内的后端中定义此转换器,如下所示:
public class DurationFormatter : IValueConverter
{
public object Convert(object value, Type targetType,
object parameter, string language)
{
string formatString = parameter as string;
if (formatString == "hoursBox")
{
return ((TimeSpan)value).Hours.ToString();
}
else if (formatString == "minutesBox")
{
return ((TimeSpan)value).Minutes.ToString();
}
else
{
return ((TimeSpan)value).Seconds.ToString();
}
}
public object ConvertBack(object value, Type targetType,
object parameter, string language)
{
string formatString = parameter as string;
if (formatString == "hoursBox")
{
return TimeSpan.FromHours(ConvertToInt32(((string)value)));//Here you get the hours value sent from textbox to backend.
}
else if (formatString == "minutesBox")
{
return TimeSpan.FromMinutes(ConvertToInt32(((string)value)));//Here you get the minutes value sent from textbox to backend.
}
else
{
return TimeSpan.FromSeconds(ConvertToInt32(((string)value)));//Here you get the seconds value sent from textbox to backend.
}
}
}
convert 方法从您的 viewModel 属性中获取数据并根据需要进行转换,然后将数据返回给您的 UI(文本框)。
convertback 方法从您的文本框中获取数据并对其进行转换,然后将其发送到您的 viewmodel 属性。
我已经向您展示了如何实现这一目标。您只需要弄清楚您想如何处理转换返回方法,我在这里编写的代码从文本框中获取一个字符串并相应地转换为 TimeSpan 对象(取决于它是否来自哪个文本框 ConverterParameter 帮助我们),然后使用 return 语句将其分配给 viewmodel Duration 属性。现在由您决定如何在将它们发送到 Duration 之前将它们组合起来。
建议
根据您的场景,您需要以某种方式组合小时、分钟和秒,然后将其分配给视图模型的持续时间。所以我建议一种可能的方法来做到这一点。
创建一个可以容纳 3 个静态属性的公共静态类,它们会在 Convert 和 ConvertBack 方法中不断更新,因此这将帮助您进行组合。
public static class DurationValues
{
public static string Hours="";
public static string Minutes="";
public static string Seconds="";
}
并具有如下的转换类。
public class DurationFormatter : IValueConverter
{
public object Convert(object value, Type targetType,
object parameter, string language)
{
string formatString = parameter as string;
if (formatString == "hoursBox")
{
string rValue = ((TimeSpan)value).Hours.ToString();
DurationValues.Hours=rValue;
return rValue;
}
else if (formatString == "minutesBox")
{
string rValue = ((TimeSpan)value).Minutes.ToString();
DurationValues.Minutes=rValue;
return rValue;
}
else
{
string rValue = ((TimeSpan)value).Seconds.ToString();
DurationValues.Seconds=rValue;
return rValue;
}
}
public object ConvertBack(object value, Type targetType,
object parameter, string language)
{
string formatString = parameter as string;
if (formatString == "hoursBox")
{
DurationValues.Hours = (string)value;
var ts = new TimeSpan (DurationValues.Hours,DurationValues.Minutes,DurationValues.Seconds);
return ts;
}
else if (formatString == "minutesBox")
{
DurationValues.Minutes = (string)value;
var ts = new TimeSpan (DurationValues.Hours,DurationValues.Minutes,DurationValues.Seconds);
return ts;
}
else
{
DurationValues.Seconds = (string)value;
var ts = new TimeSpan (DurationValues.Hours,DurationValues.Minutes,DurationValues.Seconds);
return ts;
}
}
}
另一个 IValueConverter 示例:https://www.wpf-tutorial.com/data-binding/value-conversion-with-ivalueconverter/