此示例的上下文是有四个文本框,它们总共占用时间。 1代表小时,1代表分钟,1代表秒,1代表毫秒。
第五个文本框仅以毫秒为单位保存总时间。可以在下图中看到。
我已经实现了IMultiValueConverter
,该实现应该转换4个TextBox
组件和一个属性中的转换值。属性值更改时,它还应该能够更新4个框。
当用户在输入包含转换后的输出的文本框中键入内容,然后该框失去焦点时,将更新其他4个文本框。但是,以编程方式更改属性的值(在这种情况下,通过单击按钮),不会更新4个文本框中的值。
如何使这4个文本框通过转换器更新?
在此示例中,最终目标是将总时间(以毫秒为单位)存储在属性中,并在该属性更新时通过绑定更新5个文本框。
这是转换器的代码。
using System;
using System.Globalization;
using System.Windows.Data;
namespace MultiBinding_Example
{
public class MultiDoubleToStringConverter : IMultiValueConverter
{
private const double HOURS_TO_MILLISECONDS = 3600000;
private const double MINUTES_TO_MILLISECONDS = 60000;
private const double SECONDS_TO_MILLISECONDS = 1000;
private const string ZERO_STRING = "0";
private object valBuffer = null;
/*
* values[0] is the variable from the view model
* values[1] is hours
* values[2] is the remaining whole minutes
* values[3] is the remaining whole seconds
* values[4] is the remaining whole milliseconds rounded to the nearest millisecond
*/
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
object returnVal = ZERO_STRING;
try
{
if (values != null)
{
double hoursToMilliseconds = (values[1] == null || values[1].ToString() == string.Empty) ? 0 : System.Convert.ToDouble(values[1]) * HOURS_TO_MILLISECONDS;
double minutesToMilliseconds = (values[2] == null || values[2].ToString() == string.Empty) ? 0 : System.Convert.ToDouble(values[2]) * MINUTES_TO_MILLISECONDS;
double secondsToMilliseconds = (values[3] == null || values[3].ToString() == string.Empty) ? 0 : System.Convert.ToDouble(values[3]) * SECONDS_TO_MILLISECONDS;
double totalTime = ((values[4] == null || values[4].ToString() == string.Empty) ? 0 : System.Convert.ToDouble(values[4])) + secondsToMilliseconds + minutesToMilliseconds + hoursToMilliseconds;
returnVal = totalTime.ToString();
if (values[0] == valBuffer)
{
values[0] = returnVal;
}
else
{
valBuffer = returnVal = values[0];
ConvertBack(returnVal, new Type[] { typeof(string), typeof(string), typeof(string), typeof(string), typeof(string) }, parameter, culture);
}
}
}
catch (FormatException) { }
return returnVal;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
try
{
if (value != null && value.ToString() != string.Empty)
{
double timeInMilliseconds = System.Convert.ToDouble(value);
object[] timeValues = new object[5];
timeValues[0] = value;
timeValues[1] = Math.Floor(timeInMilliseconds / HOURS_TO_MILLISECONDS).ToString();
timeValues[2] = Math.Floor((timeInMilliseconds % HOURS_TO_MILLISECONDS) / MINUTES_TO_MILLISECONDS).ToString();
timeValues[3] = Math.Floor((timeInMilliseconds % MINUTES_TO_MILLISECONDS) / SECONDS_TO_MILLISECONDS).ToString();
timeValues[4] = Math.Round(timeInMilliseconds % SECONDS_TO_MILLISECONDS, MidpointRounding.AwayFromZero).ToString();
return timeValues;
}
}
catch (FormatException) { }
return new object[] { ZERO_STRING, ZERO_STRING, ZERO_STRING, ZERO_STRING, ZERO_STRING };
}
}
}
要对此进行测试,我有一个非常简单的布局,该布局由几个Label
组件,几个TextBox
组件和一个Button
组成。
看起来像这样。
针对它的XAML如下。
<Window x:Class="MultiBinding_Example.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MultiBinding_Example"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<local:MultiDoubleToStringConverter x:Key="multiDoubleToStringConverter"/>
</Window.Resources>
<StackPanel>
<Label Content="Multi Value Converter" HorizontalAlignment="Center" FontSize="35" FontWeight="Bold" Margin="0, 25, 0, 0"/>
<Label Content="Formatted Total Time" FontWeight="Bold" FontSize="24" Margin="20, 10"/>
<Grid Margin="80, 10">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="auto"/>
</Grid.ColumnDefinitions>
<TextBox Name="Hours" HorizontalContentAlignment="Right" VerticalContentAlignment="Center" Text="0" Grid.Column="0"/>
<Label Content="Hours" Grid.Column="1" Margin="0, 0, 15, 0"/>
<TextBox Name="Minutes" HorizontalContentAlignment="Right" VerticalContentAlignment="Center" Text="0" Grid.Column="2"/>
<Label Content="Minutes" Grid.Column="3" Margin="0, 0, 15, 0"/>
<TextBox Name="Seconds" HorizontalContentAlignment="Right" VerticalContentAlignment="Center" Text="0" Grid.Column="4"/>
<Label Content="Seconds" Grid.Column="5" Margin="0, 0, 15, 0"/>
<TextBox Name="Milliseconds" HorizontalContentAlignment="Right" VerticalContentAlignment="Center" Text="0" Grid.Column="6"/>
<Label Content="Milliseconds" Grid.Column="7"/>
</Grid>
<Label Content="Unformatted Total Time" FontWeight="Bold" FontSize="24" Margin="20, 10"/>
<Grid Margin="80, 10">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="auto"/>
</Grid.ColumnDefinitions>
<TextBox HorizontalContentAlignment="Right" VerticalContentAlignment="Center" Grid.Column="0">
<TextBox.Text>
<MultiBinding Converter="{StaticResource multiDoubleToStringConverter}" Mode="TwoWay">
<Binding Path="TotalTime"/>
<Binding ElementName="Hours" Path="Text"/>
<Binding ElementName="Minutes" Path="Text"/>
<Binding ElementName="Seconds" Path="Text"/>
<Binding ElementName="Milliseconds" Path="Text"/>
</MultiBinding>
</TextBox.Text>
</TextBox>
<Label Content="Milliseconds" Grid.Column="1"/>
</Grid>
<Button Grid.Column="1" Margin="250, 20" Height="50" Content="Random Total Milliseconds" Click="RandomTime_Click"/>
</StackPanel>
</Window>
下面的代码如下。
using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows;
namespace MultiBinding_Example
{
public partial class MainWindow : Window, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private Random random = new Random();
private string totalTime;
public string TotalTime {
get => totalTime;
set {
totalTime = value;
RaisePropertyChanged();
}
}
public MainWindow()
{
InitializeComponent();
DataContext = this;
UpdateTotalTime();
}
private void RaisePropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private void RandomTime_Click(object sender, RoutedEventArgs e)
{
UpdateTotalTime();
}
private void UpdateTotalTime()
{
double percent = random.NextDouble();
double time = Math.Floor(percent * random.Next(1000, 100000000));
TotalTime = time.ToString();
}
}
}
答案 0 :(得分:1)
这并不是转换器的真正用途。
转换器将获取一组视图模型值,并将其转换为视图值以进行显示。然后,如果更改视图值,则可以将其转换回视图模型值。
在您的情况下,视图模型值是通过代码(而不是通过更改视图)来更新的,因此转换器没有理由运行ConvertBack
方法(该值已经是视图模型值! )。这是转换器不应该具有副作用的几个原因之一。
执行此操作的正确方法是将TotalTime
作为VM的属性(可能是数字或TimeSpan
而不是字符串),然后为每块。例如:
<TextBox Text="{Binding TotalTime, Converter={StaticResource TimeSecondsConverter}"/>
然后将主文本框绑定到TotalTime
。为了使TimeSecondsConverter
工作,ConvertBack
可能需要是一个多值转换器。