XAML元素之间的事件绑定

时间:2018-02-26 20:29:11

标签: wpf vb.net xaml binding

XAML

<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="200" Width="400">
<Grid>
    <TextBox x:Name="txtbox1" Height="30" Width="100" VerticalAlignment="Top"/>
    <TextBox x:Name="txtbox2" Height="30" Width="100" VerticalAlignment="Bottom"/>
</Grid>
</Window>

VB

Class MainWindow 
Private Sub txtbox1_GotFocus(sender As Object, e As RoutedEventArgs) Handles txtbox1.GotFocus
    txtbox2.Text = "Hello"
End Sub
End Class

上述代码效果很好。

我的问题:

我只想使用XAML元素绑定来执行相同操作。

换句话说,我不想从代码背后使用vb.net。

1 个答案:

答案 0 :(得分:2)

txtbox1的IsFocused或IsKeyboardFocused属性上的DataTrigger应该可以工作:

<TextBox x:Name="txtbox1" Height="30" Width="100" VerticalAlignment="Top"/>
<TextBox x:Name="txtbox2" Height="30" Width="100" VerticalAlignment="Bottom">
    <TextBox.Style>
        <Style TargetType="TextBox">
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsFocused, ElementName=txtbox1}"
                             Value="True">
                    <Setter Property="Text" Value="Hello"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBox.Style>
</TextBox>