Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net xaml中的事件顺序_.net_Xaml - Fatal编程技术网

.net xaml中的事件顺序

.net xaml中的事件顺序,.net,xaml,.net,Xaml,xaml的新功能。我有一个文本框,其中文本值和更改的事件都绑定在xaml中。我需要文本的值绑定机制在键入值时在更改的事件之前运行。我该如何执行这一点 <TextBox x:Class="WPF.AppControls.TextBoxAppControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.c

xaml的新功能。我有一个文本框,其中文本值和更改的事件都绑定在xaml中。我需要文本的值绑定机制在键入值时在更改的事件之前运行。我该如何执行这一点

<TextBox x:Class="WPF.AppControls.TextBoxAppControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    IsEnabled="{Binding Enabled}"
    Visibility="{Binding Visibility}"
    Text="{Binding Text}" 
    GotFocus="TextBox_GotFocus"
    LostFocus="TextBox_LostFocus"
    TextChanged="TextBox_TextChanged"
    KeyDown="TextBox_KeyDown">

</TextBox>

如果您需要在文本更改时更新文本,而不是在焦点丢失时更新文本,那么在WPF上,您只需将绑定替换为Text={binding Text,UpdateSourceTrigger=PropertyChanged} 在其他平台上,您将需要创建一个附加属性,类似这样的操作应该可以做到:

您可以这样使用:local:MyAttachedProperties.MyText={Binding Text}并删除正常的文本绑定

一种可能的方法:


将自定义依赖项属性绑定到TextBox.Text并为其指定PropertyChangedCallback

发布相关代码和XAML。哪个平台WPF、windows phone、winRT?为什么需要这样的行为?也许有更好的方法来做你想做的事情。打算在你列出的任何平台上使用
public class MyAttachedProperties
{
    public static readonly DependencyProperty MyTextProperty =
        DependencyProperty.RegisterAttached("MyText", typeof (string), typeof (MyAttachedProperties), new PropertyMetadata(default(string),TextChanged));

    private static void TextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {

        TextBox tb = d as TextBox;

        tb.TextChanged -= tb_TextChanged;
        tb.TextChanged += tb_TextChanged;
        string newText = e.NewValue as String;
        if (tb.Text != newText)
        {
            tb.Text = newText;
        }
    }

    static void tb_TextChanged(object sender, TextChangedEventArgs e)
    {
        TextBox tb = sender as TextBox;
        SetMyText(tb, tb.Text);

    }

    public static void SetMyText(UIElement element, string value)
    {
        element.SetValue(MyTextProperty, value);
    }

    public static string GetMyText(UIElement element)
    {
        return (string) element.GetValue(MyTextProperty);
    }
}