Binding mode of a TextBox Text property should default to TwoWay
我正在创建一个简单的MvvmLight / UWP项目。
我的模型是Article类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | 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(); } } } |
这是我的观点:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <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吗?
提前谢谢了,
朱利安
根据本文的介绍,{Binding}的默认绑定模式为OneWay,而{x:Bind}的默认绑定模式为OneTime。
因此,如果需要在绑定上将其模式明确设置为TwoWay。
有趣。 我也这样认为。 但最安全的方法是明确设置所需的绑定模式,因为它会随着属性的变化而变化。 只是一个建议。