isFoucsed上的XAML触发器C#函数

时间:2013-10-08 11:26:50

标签: c# wpf xaml triggers

我有一个问题。我创建了一个RichTextBox XAML(没有.text属性的那个,因为它似乎有两个不同的)。现在我想在文本框被聚焦时从我的.cs中调用一个函数,而当它失去焦点时我想调用另一个函数。 我已经想通了,如何触发这样的动画:

    <Style x:Key="adressBar" TargetType="{x:Type RichTextBox}">
        <Setter Property="Background" Value="transparent"></Setter>
        <Style.Triggers>
            <Trigger Property="IsFocused" Value="True">
                <Trigger.EnterActions>
                    <BeginStoryboard>
                        <Storyboard>
                            <ColorAnimation  Duration="0:0:0.2" Storyboard.TargetProperty="(TextBox.Background).(SolidColorBrush.Color)" To="#FFF5F5FE" />
                        </Storyboard>
                    </BeginStoryboard>
                </Trigger.EnterActions>
                <Trigger.ExitActions>
                    <BeginStoryboard>
                        <Storyboard>
                            <ColorAnimation  Duration="0:0:0.2" Storyboard.TargetProperty="(TextBox.Background).(SolidColorBrush.Color)" To="transparent" />
                        </Storyboard>
                    </BeginStoryboard>
                </Trigger.ExitActions>
            </Trigger>
        </Style.Triggers>
    </Style>

但我不知道如何在那里调用一个函数。 该功能基本上如下所示:

public void doStuff(){...}

我不需要解析参数! :)

1 个答案:

答案 0 :(得分:0)

您只需将处理程序附加到GotFocusLostFocus事件:

在XAML中:

<DataGrid GotFocus="DataGrid_GotFocus" LostFocus="DataGrid_LostFocus" />

在代码背后:

private void DataGrid_LostFocus(object sender, RoutedEventArgs e)
{
    doStuff();
}

private void DataGrid_GotFocus(object sender, RoutedEventArgs e)
{
    doStuff();
}