370 likes | 489 Views
BCIS 4650 Visual Programming for Business Applications. Data Binding. Additional Information re Text Formatting. Markup Extensions. Markup Extension & Syntax. Is a XAML technique for supplying complex values or values that do not map to a type; uses { }
E N D
BCIS 4650 Visual Programming for Business Applications Data Binding The University of North Texas, ITDS Dept., Dr. Vedder
Markup Extension & Syntax • Is a XAML technique for supplying complex values or values that do not map to a type; uses { } • Can be pre-defined in XAML (x:Type, x:Null, etc.), WPF, or Windows (WPF – Windows Presentation Foundation – is part of .NET and renders UI; current version 4.5) • Can be custom-defined The University of North Texas, ITDS Dept., Dr. Vedder
WPF-Specific Markup Extensions *= most often used • *StaticResource: lookup a value pre-defined elsewhere • *DynamicResource: for runtime values • *Binding: data-bound; complex syntax • RelativeSource: for sources that can change according to runtime conditions The University of North Texas, ITDS Dept., Dr. Vedder
WPF-Specific Markup Extensions, 2 • TemplateBinding: source is a template • ColorConvertedBitmap: adv. imaging • ComponentResourceKey: resource data is packaged with a custom control • ThemeResource: theme data is packaged with a custom control The University of North Texas, ITDS Dept., Dr. Vedder
Resource • An object whose property values can be used repeatedly in an app • Typically defined (XAML of C#) on page or in app, but can be elsewhere • Must have a key (string name) to access • Examples: • Brushes (for painting color, graphics) • Styles The University of North Texas, ITDS Dept., Dr. Vedder
Lookup Behavior for the Key • Page (i.e., <Page.Resources>) • App (ex., App.xaml, MyResources.xaml) • Available control themes • Resources external to the app • System (Windows 8.1) resources The University of North Texas, ITDS Dept., Dr. Vedder
App.xaml The University of North Texas, ITDS Dept., Dr. Vedder
“Go to Definition”: Your Friend “Peek” (Alt+F12) Also Your Friend The University of North Texas, ITDS Dept., Dr. Vedder
Definition is in Generic.xaml(a read-only file) The University of North Texas, ITDS Dept., Dr. Vedder
Build Your Own Resource Dictionary • Create the Dictionary as a separate file • Set as a resource in App.xaml The University of North Texas, ITDS Dept., Dr. Vedder
Retrieve App-Scope Resourceskey names must match exactly in spelling & case http://msdn.microsoft.com/en-us/library/windows/apps/hh968442.aspx The University of North Texas, ITDS Dept., Dr. Vedder
Static Resource • Retrieved only once and available for the life of the app (value does not change) • Should be defined in XAML before use The University of North Texas, ITDS Dept., Dr. Vedder
Static Resource Syntax (Most Common Usage) The University of North Texas, ITDS Dept., Dr. Vedder
Dynamic Resource • Retrieved each time requested (so the value could change during runtime) • Increases app overhead, so reduces app performance • Use StaticResource unless needed The University of North Texas, ITDS Dept., Dr. Vedder
Dynamic Resource Syntax The University of North Texas, ITDS Dept., Dr. Vedder
Static v. Dynamic Examplea lot of code missing here <Page.Resources> <SolidColorBrush Color="LightBlue" x:Key="buttonBackground" /> </Page.Resources> <StackPanel Name="stkPanel"> <Button Name="Button1" Content="Button1" Background="{StaticResourcebuttonBackground}"/> <Button Name="Button2" Content="Button2" Background="{DynamicResourcebuttonBackground}"/> <Button Name="Button3" Content="Button3" Background="{StaticResourcebuttonBackground}"/> </StackPanel> ----- Code behind---- void StaticAndDynamicResources_Loaded(object sender, RoutedEventArgs e) { stkPanel.Resources["buttonBackground"] = Brushes.Yellow; } The University of North Texas, ITDS Dept., Dr. Vedder
Binding (Data Binding) • Connecting a source property’s value with a target property’s value; the connection stays open during app’s life • Use when hard-coding values to properties is not a good idea (ex. runtime settings (ex., slider), animations) • Is the Binding class in XAML & C# • Posts changes automatically eitherway • Supports complex data values The University of North Texas, ITDS Dept., Dr. Vedder
Common Binding Scenariosinclude • Binding a ListBox to a set of headlines • Binding an Image to a photo of the current user • Is OK to bind one source property to many target properties The University of North Texas, ITDS Dept., Dr. Vedder
The Binding Target Property Must Be A Dependency Property • A DP is a ‘super property’ • A DP relies on multiple providers for its value at any given time • A DP adds value to a static property • Self-contained validation • Change notification • Inheritance of property values • Support for multiple providers The University of North Texas, ITDS Dept., Dr. Vedder
Data-Binding Model & Example Binding sources include: • UI elements • Any object in a list • ADO.NET DataTables, .JSON file, etc. • Web services The University of North Texas, ITDS Dept., Dr. Vedder
“Multiple Providers” Rankingnot all possibilities shown here • Coertion (RT data changes prop. value) • Active animations • Local value (a binding or resource) • Template properties (TemplateBinding) • Implicit style (i.e., not named) (Source must be on the same page or in the app) The University of North Texas, ITDS Dept., Dr. Vedder
“Multiple Providers” Ranking, 2not all possibilities shown here • Style triggers Triggerconditionally applies a value to a Style prop. • Template triggers • Style setters Setter applies a value to a Style property • Default (theme) style • Property value inheritance The University of North Texas, ITDS Dept., Dr. Vedder
Three Different Values for Background • Local value=Red, so that’s the color • If just <Button> • Triggers take precedence over Setters, so • Blue if mouse over it, green otherwise The University of North Texas, ITDS Dept., Dr. Vedder
So, How To ID a Dependency Property? • A DP name must end with ‘Property’ • Intellisense will show DPs • Search property In MSDN library; see if there is a ‘Dependency Property Information’ section The University of North Texas, ITDS Dept., Dr. Vedder
But How Do I Write a DP? • For newbies, the best course is to write the complete name, so that you understand better what you are doing, ex., TextBox.TextProperty • BUT… frequently coders use a ‘semantic shortcut’ and just write the first part of the DP and let the compiler figure it out, ex., TextBox.Text • (The ordinary (CLR) prop is ‘backed’ by the DP) The University of North Texas, ITDS Dept., Dr. Vedder
The Binding Object’s Mode Prop. Controls the Interaction Behavior • OneWay • OneTime • TwoWay • OneWayToSource The University of North Texas, ITDS Dept., Dr. Vedder
Target Update Can be Automatic, But Updating the Source? UpdateSourceTrigger property values • PropertyChanged – asap, ex., online chat • LostFocus – when user moves away from target object, ex., data validation The University of North Texas, ITDS Dept., Dr. Vedder
Simple Binding Example in XAML • Source object = MyData • Source property = ColorName • Target object = Button • Target DP = Background(.Property) (Button inherits DataContext value) The University of North Texas, ITDS Dept., Dr. Vedder
Simple 2-Way Binding Example • DP TextBox.Text defaults to 2-way, so no Mode property setting needed • Note use of ElementName property The University of North Texas, ITDS Dept., Dr. Vedder
DataContext Property • Useful when you want many elements inside a container to bind to the same source • Set the DataContext property in the parent container, then use inheritance The University of North Texas, ITDS Dept., Dr. Vedder
Sample DataContext Usage • Length refers to string fed to TextBlock.Text • {Binding} with no “Path=” means the entire source object (in this case, just a string) The University of North Texas, ITDS Dept., Dr. Vedder
ITDS Logo / Mood Slide The University of North Texas, ITDS Dept., Dr. Vedder