繁体   English   中英

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

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

我正在创建一个简单的MvvmLight / UWP项目。

我的模型是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();
        }
    }
}

这是我的观点:

<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>

注意两个文本框绑定中的Mode = TwoWay参数。 如果我不使用它,则会得到一个OneWay绑定。

TextBox.Text属性的绑定不应该默认为TwoWay吗?

提前谢谢了,
朱利安

根据文章,默认为{}绑定结合模式是单向的,而{X:}绑定有一次性的默认模式。

因此,如果需要在绑定上将其模式明确设置为TwoWay

有趣。 我也是这么想的。 但最安全的方法是明确设置所需的绑定模式,因为它会随着属性的变化而变化。 只是一个建议。

暂无
暂无

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

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