如何在 wpf 中将多个文本框与单个文本块绑定

How to bind multiple textboxes with a single textblock in wpf

我有 10 个 textboxes,我希望在 lost_focus UpdateSourceTrigger 属性.[=14= 上的单个 textblock 中显示添加内容]

如果您需要更新 TextBox 失去焦点事件的总和,您可以使用经典事件。这是 XAML(我只使用了四个文本框,但很容易扩展):

<StackPanel>
    <TextBox Name="txt01" Margin="3" HorizontalAlignment="Stretch" LostFocus="txt_LostFocus" TextChanged="txt_TextChanged" />
    <TextBox Name="txt02" Margin="3" HorizontalAlignment="Stretch" LostFocus="txt_LostFocus" TextChanged="txt_TextChanged" />
    <TextBox Name="txt03" Margin="3" HorizontalAlignment="Stretch" LostFocus="txt_LostFocus" TextChanged="txt_TextChanged" />
    <TextBox Name="txt04" Margin="3" HorizontalAlignment="Stretch" LostFocus="txt_LostFocus" TextChanged="txt_TextChanged" />
    <TextBlock Name="sum" Margin="3,10,3,3" />
</StackPanel>

在代码中你有事件处理程序:

private void txt_LostFocus(object sender, RoutedEventArgs e)
{
    int value1;
    int value2;
    TextBox textBox = (TextBox)sender;

    if (textBox.Tag is bool && (bool)textBox.Tag)
    {

        if (Int32.TryParse(textBox.Text, out value1))
        {
            if (String.IsNullOrEmpty(sum.Text))
            {
                sum.Text = textBox.Text;
            }
            else
            {
                Int32.TryParse(sum.Text, out value2);
                sum.Text = Convert.ToString(value1 + value2);
            }
        }

        textBox.Tag = false;
    }
}

private void txt_TextChanged(object sender, TextChangedEventArgs e)
{
    TextBox textBox = (TextBox)sender;
    textBox.Tag = true;
}

另一方面,如果你可以放弃 "LostFocus" 要求,你可以使用 MultiBinding(在这种情况下它只适用于 "PropertyChanged mode",因为文本框现在是来源):

<StackPanel>
    <TextBox Name="txt01" Margin="3" HorizontalAlignment="Stretch" />
    <TextBox Name="txt02" Margin="3" HorizontalAlignment="Stretch" />
    <TextBox Name="txt03" Margin="3" HorizontalAlignment="Stretch" />
    <TextBox Name="txt04" Margin="3" HorizontalAlignment="Stretch" />
    <TextBlock Name="sum" Margin="3,10,3,3">
        <TextBlock.Text>
            <MultiBinding Converter="{StaticResource AddValueConverter}" Mode="OneWay">
                <MultiBinding.Bindings>
                    <Binding ElementName="txt01" Path="Text" />
                    <Binding ElementName="txt02" Path="Text" />
                    <Binding ElementName="txt03" Path="Text" />
                    <Binding ElementName="txt04" Path="Text" />
                </MultiBinding.Bindings>
            </MultiBinding>
        </TextBlock.Text>
    </TextBlock>
</StackPanel>

你只需要写一个简单的转换器:

public class AddValueConverter : IMultiValueConverter
{

    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        int sum = 0;
        int result;

        foreach(object value in values)
        {
            if (Int32.TryParse(System.Convert.ToString(value), out result))
            {
                sum += result;
            }
        }

        return System.Convert.ToString(sum);
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}