v 1.0 programming iii. converters resources. v 1.0Óe-nik, 2014 converters data conversion: –can...

8
V 1.0 Programming III. Converters Resources

Upload: laurence-wells

Post on 17-Jan-2016

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 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

V 1.0

Programming III.

ConvertersResources

Page 2: 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

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(): sourcetarget (VM UI)• ConvertBack(): targetsource (UIVM)• After the ConvertBack(), the getter is executed (along with the Convert())

2

<ListBox x:Name=“lbColors" BorderBrush="{Binding ElementName=lbColors, Path=SelectedItem}” .../>

Page 3: 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

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” ... > <Grid> <Grid.Resources> <current:AgeValueConverter x:Key=“AgeConverter"/> </Grid.Resources> <Label Content=“Name of the person:" .../> <Label Content=“Approximate age:" .../> <Label Content="{Binding Name}” .../> <Label Content="{Binding Age, Converter={StaticResource AgeConverter}}” ... /> </Grid></Window>

Reference the .NET namespace

Import the .NET converter class into the XAML

Assign the converter to the binding

Page 4: 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

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(); } }

Page 5: 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

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 ... <Grid> <Grid.Resources> <current:AgeValueConverter x:Key=“AgeConverter"/> </Grid.Resources> ... </Grid></Window>

Import the class into the resource dictionary of the Grid

Page 6: 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

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

<Window ...> <Grid> <Grid.Resources> <SolidColorBrush x:Key=“MyColor" Color="Azure"/> </Grid.Resources> ... <Label Background="{StaticResource ResourceKey=MyColor}" Content=“Text with nice color..." .../> <ListBox Background="{StaticResource ResourceKey=MyColor}" .../> </Grid></Window>

Page 7: 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

V 1.0 ÓE-NIK, 2014

<Image Source="LockScreen___0600_0337.jpg" />

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

Page 8: 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

V 1.0 ÓE-NIK, 2014

Feladat

• Team = center + left wing + right wing + 2 defender + goalie

• Name = only characters8