簡體   English   中英

如何在WPF中將多個文本框與單個文本塊綁定

[英]How to bind multiple textboxes with a single textblock in wpf

我有10個textboxes ,我希望在lost_focus UpdateSourceTrigger屬性的單個textblock lost_focus顯示其他內容。

如果您需要更新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;
}

另一方面, 如果您可以放棄“ LostFocus”需求 ,則可以使用MultiBinding (在這種情況下,它僅在“ PropertyChanged模式”下有效,因為TextBoxes現在是源):

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM