Tag: ble RTB one INF stack content result slider value
This blog will show the content of WPF databinding.
First look at an overview of the WPF Data binding,
The binding source can be any CLR object, or XML file, and so on, the binding target needs to have a dependency property. This allows data Binding to be performed. Take a look at the following example,
C#
public partial class Mainwindow:window {public MainWindow () { InitializeComponent (); DataContext = new Person () {Name = ' Tom ', age = +}; } } public class person {public string Name {get; set;} public int Age {get; set;} }
Xaml:
<Grid> <StackPanel> <textblock text= "{Binding Name}" margin= "10,5"/> <textblock text= "{Binding age}" margin= "10,5"/> </StackPanel> </Grid>
Operation Result:
Here are some examples of data Binding,
1. In XAML, we can specify the ElementName and path properties of the binding target to get the data of the binding source, for example,
Xaml:
<StackPanel> <slider x:name= "_slider" minimum= "0" maximum= "" "value=" "/> <textbox text= " {Binding Elementname=_slider,path=value} "/> </StackPanel>
Operation Result:
In the above XAML code, the Value property of the slider is binding to a TextBox.
As you can see from the GIF above, when you drag the slider, the value in the TextBox changes as the slider's value changes, and when you enter a value in the TextBox, the value of the slider changes when you press the TAB key to lose focus. This is because the default binding mode for the TextBox is TwoWay, and Updatesouretrigger defaults to LostFocus. Take a look at the bindingmode below,
There is a binding Mode in the illustration,
OneWay, the data flow is from source to target;
TwoWay, the data can be from source to target or from target to source,
OneWayToSource In contrast to OneWay,
OneTime, which means that after data binding occurs, the data source changes anyway, and the value bound in target does not change.
Let's illustrate the 4 kinds of bindings by example.
Xaml:
<Grid> <Grid.RowDefinitions> <rowdefinition height= "Auto"/> <rowdefiniti On height= "Auto"/> <rowdefinition height= "Auto"/> <rowdefinition height= "Auto"/> </Grid.RowDefinitions> <!--OneWay binding--> <StackPanel> <textblock Text = "Binding mode:oneway"/> <slider x:name= "_slider" minimum= "0" maximum= "[] value=" "/>" Lt TextBox text= "{Binding elementname=_slider,path=value,mode=oneway}"/> </StackPanel> <!-- OneTime binding--> <stackpanel grid.row= "1" margin= "0,20" > <textblock text= "Binding mode:on ETime "/> <slider x:name=" _slider1 "minimum=" 0 "maximum=" [] "value="/> <textbox text= "{Binding elementname=_slider1,path=value,mode=onetime}"/> </StackPanel> <!--Onewaytosou Rce binding--> <!--the fontsize default value for the textbox is 12--> <stackpanel grid.row= "2" > <textblock text= "Binding Mod E:onewaytosource "/> <slider x:name=" _slider2 "minimum=" 0 "maximum=" + "value="/> <t Extbox fontsize= "{Binding elementname=_slider2,path=value, Mode=onewaytosource}" text= "{Binding Relat Ivesource={relativesource self},path=fontsize} "/> </StackPanel> <!--TwoWay binding--> <stackpanel grid.row= "3" margin= "0,20" > <textblock text= "Binding mode:twoway"/> <sli Der X:name= "_slider3" minimum= "0" maximum= "+ value=" "/> <textbox text=" {Binding Elementname=_slider3 , Path=value,mode=twoway} "/> </StackPanel> </Grid>
Operation Result:
In the project development according to the actual need to choose a different binding Mode, if some data is only displayed at the start of the program, after the face of data modification, do not need to update, you can choose onetime mode, if the data in real-time display, you can choose OneWay mode If you need to synchronize the data changes, we choose TwoWay Mode. The appropriate binding mode optimizes the performance of the program.
Next look at Updatesourcetrigger,updatesourcetrigger has the following several values,
1. LostFocus, such as when the textbox loses focus, updates the source;
2. PropertyChanged, for example, when input in a textbox, source is updated synchronously;
3. Explict, call Updatesource method that needs to be displayed;
Let's look at another concept in the binding converter; For example, there is a text box showing the current temperature, if the temperature is greater than 35, the foreground color of the text box is set to red, if the temperature is less than 0, then the display is blue;
Xaml:
<Window.Resources> <local:temperaturetobrushcoverter hot= "cold=" 0 "x:key=" Temperaturetobrushcoverter "/> </Window.Resources> <Grid> <textbox text=" width= "Height=" " foreground=" {Binding path=text,relativesource={relativesource self}, converter={ StaticResource Temperaturetobrushcoverter}} "/> </Grid>
C#:
[Valueconversion (typeof (Double), typeof (Brush))] public class Temperaturetobrushcoverter:ivalueconverter { Public double hot {get; set;} Public double Cold {get; set;} Public Temperaturetobrushcoverter () {} public temperaturetobrushcoverter (double hot,double Cold) : this () {hot = hot; Cold = cold; public object Convert (object value, Type targetType, object parameter, CultureInfo culture) {D Ouble temp; Brush brush = brushes.black; if (Double.tryparse (string) value,out temp)) {if (temp > Hot) { Brush = brushes.red; } else if (Temp < Cold) {brush = Brushes.blue; }} return brush; public object Convertback (object value, Type targetType, object parameter, CultUreinfo culture) {throw new NotImplementedException (); } }
Operation Result:
Note To reference converter in XAML.
Finally, let's take a look at the data validation in the databinding, and look at an example where the age of the person object is bound to a textbox, and can be modified for ages, which is a meaningful value, if it is less than 0 or greater than 120, We all think that it is illegal value, give the corresponding hint. Take a look at the following code:
Xaml:
<Grid> <textbox width= "height=" > <TextBox.Text> <binding path= "age" Updatesourcetrigger= "propertychanged" > <Binding.ValidationRules> <local:agevalidationrule validationstep= "Rawproposedvalue"/> </Binding.ValidationRules> </Binding> </ Textbox.text> <!--Error template--> <Validation.ErrorTemplate> <controltemplate > <StackPanel> <AdornedElementPlaceholder/> <textblock text= "{Binding [0]. Errorcontent} "foreground=" Red "/> </StackPanel> </ControlTemplate> </ validation.errortemplate> </TextBox> </Grid>
C#:
public partial class Mainwindow:window {public MainWindow () { InitializeComponent (); DataContext = new Person () {age = +}; } } public class person {public int age {get; set;} } public class Agevalidationrule:validationrule {public override Validationresult Validate (object value, CultureInfo CultureInfo) { int age; if (int. TryParse (String) Value,out age) { if (age< 0 | | | >) { return new Validationresult ( False, "This was Invalide age."); else { return new Validationresult (true, null); } } return new Validationresult (false, "please input valid.");}
Operation Result:
The content of this blog is here, but the content of the data binding in WPF is much more than that, and will be shown in a later blog post.
Thank you for reading! Code click here to download.
Data binding for the WPF QuickStart series