BindingMode Enum
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
The direction of changes propagation for bindings.
public enum class BindingMode
public enum BindingMode
type BindingMode =
Public Enum BindingMode
- Inheritance
-
BindingMode
Fields
Default | 0 | When used in Bindings, indicates that the Binding should use the DefaultBindingMode. When used in BindableProperty declaration, defaults to BindingMode.OneWay. |
OneTime | 4 | Indicates that the binding will be applied only when the binding context changes and the value will not be monitored for changes with |
OneWay | 2 | Indicates that the binding should only propagate changes from source (usually the View Model) to target (the BindableObject). This is the default mode for most BindableProperty values. |
OneWayToSource | 3 | Indicates that the binding should only propagate changes from target (the BindableObject) to source (usually the View Model). This is mainly used for read-only BindableProperty values. |
TwoWay | 1 | Indicates that the binding should propagates changes from source (usually the View Model) to target (the BindableObject) in both directions. |
Remarks
The following examples shows some BindingMode use cases.
public class PersonViewModel
{
public string Name { get; set; }
public string Company { get; set; }
}
Label label;
PersonViewModel viewmodel;
//BindingMode.OneWay
label = new Label ();
label.BindingContext = viewmodel = new PersonViewModel ();
label.SetBinding<PersonViewModel> (Label.TextProperty, vm => vm.Name, mode: BindingMode.OneWay);
viewmodel.Name = "John Doe";
Debug.WriteLine (label.Text); //prints ""
label.Text = "Foo";
Debug.WriteLine (viewmodel.Name); //prints "John Doe"
//BindingMode.TwoWay
label = new Label ();
label.BindingContext = viewmodel = new PersonViewModel ();
label.SetBinding<PersonViewModel> (Label.TextProperty, vm => vm.Name, mode: BindingMode.TwoWay);
viewmodel.Name = "John Doe";
Debug.WriteLine (label.Text); //prints "John Doe"
label.Text = "Foo";
Debug.WriteLine (viewmodel.Name); //prints "Foo"
//BindingMode.OneWayToSource
label = new Label ();
label.BindingContext = viewmodel = new PersonViewModel ();
label.SetBinding<PersonViewModel> (Label.TextProperty, vm => vm.Name, mode: BindingMode.OneWayToSource);
viewmodel.Name = "John Doe";
Debug.WriteLine (label.Text); //prints ""
label.Text = "Foo";
Debug.WriteLine (viewmodel.Name); //prints "Foo"
Applies to
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈