c#oneway元素绑定更新失去焦点

时间:2017-12-31 14:45:57

标签: .net visual-studio

我有一个任务,我需要单独绑定以构建一个名称,但每当其中一个文本框失去焦点时,就会构建全名。 updateSourceTrigger = LostFocus不起作用,因为它是nog twoway绑定。 是否有可能单独使用?

<Window x:Class="name_binder.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:name_binder"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="575">
    <Grid>
        ...
        <StackPanel Orientation="Vertical" Grid.Column="1" Grid.Row="2">
            <TextBox Name="firstNameTextBox" Margin="7.5" Height="35" FontSize="20" Text="&lt;Enter first name>" GotFocus="TextBox_gotFocus"></TextBox>
            <TextBox Name="LastNameTextBox" Margin="7.5" Height="35" FontSize="20" Text="&lt;Enter last name>" GotFocus="TextBox_gotFocus"></TextBox>
            <StackPanel Orientation="Horizontal">
                <TextBox Margin="7.5" Height="35" FontSize="20" BorderThickness="0" Text="{Binding Path=Text, ElementName=firstNameTextBox, Mode=OneWay, UpdateSourceTrigger=LostFocus}"/>
                <TextBox Margin="7.5" Height="35" FontSize="20" BorderThickness="0" Text="{Binding Path=Text, ElementName=LastNameTextBox, Mode=OneWay, UpdateSourceTrigger=LostFocus}"/>
            </StackPanel>
        </StackPanel>
    </Grid>
</Window>

1 个答案:

答案 0 :(得分:0)

我假设这是WPF?

取决于您使用的设计类型:

<强>一

您可以处理LostFocus事件:

<TextBox x:Name="tb" Text="{Binding SomeText, Mode=OneWay}" LostFocus="tb_LostFocus">

然后您可以访问((TextBox)sender).Text

<强>两个

您可以在System.Windows.Interactivity事件发生时使用LostFocus执行命令,将文本作为参数传递(您可能需要添加对System.Windows.Interactivity的引用你的项目):

<Window ... xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" ... >
...
<TextBox x:Name="tb" Text="{Binding SomeText, Mode=OneWay}">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="LostFocus">
            <i:InvokeCommandAction Command="{Binding SomeCommand}"
                                   CommandParameter="{Binding Text, ElementName=tb}" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
</TextBox>