简体   繁体   English

TextBox Text属性的绑定模式应默认为TwoWay

[英]Binding mode of a TextBox Text property should default to TwoWay

I'm creating a simple MvvmLight/UWP project. 我正在创建一个简单的MvvmLight / UWP项目。

My model is the Article class : 我的模型是Article类:

public class Article : ObservableObject
{
    public Guid Id { get; set; }

    string référence;
    public string Référence
    {
        get { return référence; }
        set
        {
            if (référence == value)
                return;
            référence = value;
            RaisePropertyChanged();
        }
    }

    string désignation;
    public string Désignation
    {
        get { return désignation; }
        set
        {
            if (désignation == value)
                return;
            désignation = value;
            RaisePropertyChanged();
        }
    }
}

And this is my view : 这是我的观点:

<Page
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:UniversalTest1.UWP.Articles"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    x:Class="UniversalTest1.UWP.Articles.Article_Detail"
    mc:Ignorable="d"
    xmlns:vm="clr-namespace:UniversalTest1.Data.ViewModels.Articles;assembly=UniversalTest1.Data"
    d:DataContext="{d:DesignInstance Type=vm:ArticleViewModel, IsDesignTimeCreatable=True}">

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <TextBlock Text="Référence :"   HorizontalAlignment="Left" Margin="24,15,0,0" VerticalAlignment="Top"/>
        <TextBlock Text="Désignation :" HorizontalAlignment="Left" Margin="10,52,0,0" VerticalAlignment="Top"/>

        <TextBox Text="{Binding Article.Référence, Mode=TwoWay}" HorizontalAlignment="Left" Margin="100,8,0,0" VerticalAlignment="Top" Width="300"/>
        <TextBox Text="{Binding Article.Désignation, Mode=TwoWay}" HorizontalAlignment="Left" Margin="100,45,0,0" VerticalAlignment="Top" Width="500"/>

        <Button Content="Sauver" Command="{Binding SauverCommand}" HorizontalAlignment="Left" Margin="102,84,0,0" VerticalAlignment="Top"/>
    </Grid>
</Page>

Notice the Mode=TwoWay parameter in the binding for the 2 text boxes. 注意两个文本框绑定中的Mode = TwoWay参数。 If I don't use it, I get a OneWay binding. 如果我不使用它,则会得到一个OneWay绑定。

Shouldn't the binding for a TextBox.Text property default to TwoWay ? TextBox.Text属性的绑定不应该默认为TwoWay吗?

Many thanks in advance, 提前谢谢了,
Julien 朱利安

According to this article, the default Binding mode for {Binding} is OneWay while the {x:Bind} has a default mode of OneTime. 根据文章,默认为{}绑定结合模式是单向的,而{X:}绑定有一次性的默认模式。

So, you do need to explicitly set your mode to TwoWay if you need that on your bindings. 因此,如果需要在绑定上将其模式明确设置为TwoWay

Interesting. 有趣。 I thought so too. 我也是这么想的。 But safest is to explicitly set what binding mode you want as it changes from property to property. 但最安全的方法是明确设置所需的绑定模式,因为它会随着属性的变化而变化。 Just a recommendation. 只是一个建议。

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

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