简体   繁体   中英

Binding mode of a TextBox Text property should default to TwoWay

I'm creating a simple MvvmLight/UWP project.

My model is the Article class :

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. If I don't use it, I get a OneWay binding.

Shouldn't the binding for a TextBox.Text property default to 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.

So, you do need to explicitly set your mode to TwoWay if you need that on your bindings.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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