V 1.0 Programming III. Converters Resources
V 1.0ÓE-NIK, 2014 Converters Data conversion: –Can be automatic! E.g.: SolidColorBrush string, int string –Otherwise, we have to use a class that implements the IValueConverter interface (System.Windows.Data) –Two methods: Convert(), ConvertBack() Convert(): source target (VM UI) ConvertBack(): target source (UI VM) After the ConvertBack(), the getter is executed (along with the Convert()) 2 <ListBox x:Name=“lbColors" BorderBrush= "{Binding ElementName=lbColors, Path=SelectedItem}”.../>
V 1.0ÓE-NIK, 2014 Converters Person class: –Name (string) –Age (int) Binding: pesron.Age label.Content 3 <Window... xmlns:current="clr-namespace:WpfApplication9”... > Reference the.NET namespace Import the.NET converter class into the XAML Assign the converter to the binding
V 1.0ÓE-NIK, 2014 Converters Converter class –In this form, only one-way conversion is required –Look out for the data types! 4 class AgeValueConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { int kor = (int)value; if (kor < 18) return "child"; else if (kor < 30) return "young"; else if (kor < 50) return "middle-aged"; else if (kor < 65) return "older"; else return "old"; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); }
V 1.0ÓE-NIK, 2014 Resources A resource is a class/instance that we can reuse in our application multiple times Types: –Logical/class resources: arbitrary instances (created in XAML) –Binary resources: images, icons, lookup tables... The logical resources are stored in the resource dictionary, each of them has a unique name (x:Key) –Resources property (in FrameworkElement descendants) Contained controls will receive the parent controls’ resources 5 <Window Import the class into the resource dictionary of the Grid
V 1.0ÓE-NIK, 2014 Resources Logical/class resources can be used in other places: –Brushes, colors, styles, templates, converters –Arbitrary instances (e.g. arrays) –E.g.: color (brush) in the resource dictionary: Types: –StaticResource: created once when the XAML is loaded, read-only –DynamicResource: loaded in run-time, it can be modified 6... <Label Background="{StaticResource ResourceKey=MyColor}" Content=“Text with nice color...".../>
V 1.0ÓE-NIK, 2014 Resources Binary resources: –The project can include arbitrary files that the application needs –To include a file as a binary resource, it has to be added to the project, then in the properties window we have to change the “Build Action” of the file Resource: the compiler will embed the data into the.NET assembly Content: the files will remain external files, we have to change the “copy to output directory” setting as welll We can reference them in the XAML with their filename 7
V 1.0ÓE-NIK, 2014 Feladat Team = center + left wing + right wing + 2 defender + goalie Name = only characters 8