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

时间:2015-03-05 17:01:08

标签: c# wpf binding

我有10个textboxes,我希望在textblock lost_focus属性的单个UpdateSourceTrigger中显示此内容。

1 个答案:

答案 0 :(得分:1)

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

<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;
}

另一方面,如果你可以放弃&#34; LostFocus&#34;要求,您可以使用MultiBinding(在这种情况下,它仅适用于&#34; PropertyChanged模式&#34;,因为TextBox现在是源代码):

<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();
    }
}