繁体   English   中英

Windows Phone TwoWay绑定不起作用

[英]Windows Phone TwoWay binding not working

实际上2种方式的绑定都有效,但是有一个问题。

我在应用栏上有一个按钮。 我也有一个带有TwoWay绑定的文本框。 现在,如果我在文本框中键入内容,并且从文本框中移出焦点(通过按返回键关闭键盘),则文本框文本绑定到的属性将更新。

但是,如果我在不关闭键盘的情况下按下AppBar按钮,则该属性不会得到更新。

有解决这个问题的简单方法吗?

非常感谢所有帮助。 谢谢!

编辑:我尝试了this.focus在AppBar按钮上单击,但仍然没有运气

编辑2:

这是我的代码-

<StackPanel>
    <TextBlock Text="Title" FontSize="{StaticResource PhoneFontSizeMediumLarge}" Margin="15,0,0,0"/>
    <TextBox Name="TitleTB" Text="{Binding Title, Mode=TwoWay}" />
    <TextBlock Text="Description" FontSize="{StaticResource PhoneFontSizeMediumLarge}" Margin="15,0,0,0"/>
    <TextBox Name="DescriptionTB" Text="{Binding Description, Mode=TwoWay}" AcceptsReturn="True" MaxHeight="300" VerticalScrollBarVisibility="Auto" />
</StackPanel>

.cs代码-

public CreateTaskPage()
    {
        InitializeComponent();

        M1 = new MyClass { Description = "Description", Title = "title1" };
        this.DataContext = M1;
    }
private void ApplicationBarIconButton_Click(object sender, EventArgs e)
    {
        //save - I change the text in the textbox from title1 to title123 suppose
        // But it still shows title1 if I click the appbar button without closing the keyboard
        this.Focus();

        MessageBox.Show(M1.Title);
    }

编辑3:MyClass代码-

public class MyClass : INotifyPropertyChanged
{
    private string title;

    private string description;

    public string Title
    {
        get { return title; }
        set
        {
            title = value;
            OnPropertyChanged("Title");
        }
    }

    public string Description
    {
        get { return description; }
        set
        {
            description = value;
            OnPropertyChanged("Description");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }
}

为了确保您是否正确声明了MyClass的属性,如下所示?

class MyClass {
    public String Description { get; set; }
    public String Title { get; set; }
}

尝试这个:

private void ApplicationBarIconButton_Click(object sender, EventArgs e)
{
        BindingExpression expression = TitleTB.GetBindingExpression(TextBox.TextProperty);
        MessageBox.Show("Before UpdateSource, Test = " + M1.Title);
        expression.UpdateSource();
        MessageBox.Show("After UpdateSource, Test = " + M1.Title);
}

有关绑定的更多信息,请转到此处Windows Phone的数据绑定

为什么不在ApplicationBarIconButton的click事件中将焦点从文本框转移到其他控件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM