首页 > 解决方案 > 属性值更改时更新文本框 - WPF

问题描述

此示例的上下文是有四个文本框,它们包含总时间量。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();
        }
    }
}

标签: c#wpfbinding

解决方案


这不是转换器的真正用途。

转换器采用一组视图模型值并将它们转换为视图值以进行显示。然后,如果视图值发生变化,它可以将它们转换回视图模型值。

在您的情况下,视图模型值是通过代码更新的(而不是通过更改视图),因此转换器没有理由运行该ConvertBack方法(该值已经是视图模型值!)。这是转换器不应该有副作用的几个原因之一。

正确的方法是TotalTime在 VM 上拥有一个属性(可能是一个数字,TimeSpan而不是你拥有的字符串),然后为每个部分执行单独的转换器。例如:

 <TextBox Text="{Binding TotalTime, Converter={StaticResource TimeSecondsConverter}"/>

然后主文本框将绑定到TotalTime. TimeSecondsConverter可能需要成为一个多值转换器才能ConvertBack工作。


推荐阅读