Skip to content
This repository was archived by the owner on Apr 29, 2024. It is now read-only.

Commit 965333c

Browse files
committed
Added two multibinding code examples.
1 parent 817221c commit 965333c

File tree

3 files changed

+219
-0
lines changed

3 files changed

+219
-0
lines changed

DataBindingDemos/DataBindingDemos/DataBindingDemos/Views/MainPage.xaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,21 @@
130130
Command="{Binding NavigateCommand}"
131131
CommandParameter="{x:Type local:MultiBindingStringFormatPage}" />
132132

133+
<TextCell Text="String Formatting (Code)"
134+
Detail="Combine strings from a MultiBinding"
135+
Command="{Binding NavigateCommand}"
136+
CommandParameter="{x:Type local:MultiBindingStringFormatCodePage}" />
137+
133138
<TextCell Text="Multi Value Converters"
134139
Detail="Use a multi value converter to set the CheckBox.IsChecked property"
135140
Command="{Binding NavigateCommand}"
136141
CommandParameter="{x:Type local:MultiBindingConverterPage}" />
137142

143+
<TextCell Text="Multi Value Converters (Code)"
144+
Detail="Use a multi value converter to set the CheckBox.IsChecked property"
145+
Command="{Binding NavigateCommand}"
146+
CommandParameter="{x:Type local:MultiBindingConverterCodePage}" />
147+
138148
<TextCell Text="Nest Multi Bindings"
139149
Detail="Nest multi bindings to set the CheckBox.IsChecked property"
140150
Command="{Binding NavigateCommand}"
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
using System.Collections.ObjectModel;
2+
using Xamarin.Forms;
3+
4+
namespace DataBindingDemos
5+
{
6+
public class MultiBindingConverterCodePage : ContentPage
7+
{
8+
public MultiBindingConverterCodePage()
9+
{
10+
BindingContext = new GroupViewModel();
11+
12+
Grid grid = new Grid { Margin = new Thickness(20) };
13+
14+
grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Auto) });
15+
grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Auto) });
16+
grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Auto) });
17+
grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Auto) });
18+
grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Auto) });
19+
grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Auto) });
20+
21+
grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(0.75, GridUnitType.Star) });
22+
grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(0.25, GridUnitType.Star) });
23+
24+
grid.Children.Add(new Label { Text = "Employee", FontAttributes = FontAttributes.Bold });
25+
grid.Children.Add(new Label { Text = "Can drive?", FontAttributes = FontAttributes.Bold }, 1, 0);
26+
27+
Label employee1 = new Label { VerticalTextAlignment = TextAlignment.Center };
28+
employee1.SetBinding(Label.TextProperty, "Employee1.FullName");
29+
30+
CheckBox checkBox1 = new CheckBox { HorizontalOptions = LayoutOptions.End };
31+
checkBox1.SetBinding(CheckBox.IsCheckedProperty, new MultiBinding
32+
{
33+
Bindings = new Collection<BindingBase>
34+
{
35+
new Binding("Employee1.IsOver16"),
36+
new Binding("Employee1.HasPassedTest"),
37+
new Binding("Employee1.IsSuspended", converter: new InverterConverter())
38+
},
39+
Converter = new AllTrueMultiConverter()
40+
});
41+
42+
Label employee2 = new Label { VerticalTextAlignment = TextAlignment.Center };
43+
employee2.SetBinding(Label.TextProperty, "Employee2.FullName");
44+
45+
CheckBox checkBox2 = new CheckBox { HorizontalOptions = LayoutOptions.End };
46+
checkBox2.SetBinding(CheckBox.IsCheckedProperty, new MultiBinding
47+
{
48+
Bindings = new Collection<BindingBase>
49+
{
50+
new Binding("Employee2.IsOver16"),
51+
new Binding("Employee2.HasPassedTest"),
52+
new Binding("Employee2.IsSuspended", converter: new InverterConverter())
53+
},
54+
Converter = new AllTrueMultiConverter()
55+
});
56+
57+
Label employee3 = new Label { VerticalTextAlignment = TextAlignment.Center };
58+
employee3.SetBinding(Label.TextProperty, "Employee3.FullName");
59+
60+
CheckBox checkBox3 = new CheckBox { HorizontalOptions = LayoutOptions.End };
61+
checkBox3.SetBinding(CheckBox.IsCheckedProperty, new MultiBinding
62+
{
63+
Bindings = new Collection<BindingBase>
64+
{
65+
new Binding("Employee3.IsOver16"),
66+
new Binding("Employee3.HasPassedTest"),
67+
new Binding("Employee3.IsSuspended", converter: new InverterConverter())
68+
},
69+
Converter = new AllTrueMultiConverter()
70+
});
71+
72+
Label employee4 = new Label { VerticalTextAlignment = TextAlignment.Center };
73+
employee4.SetBinding(Label.TextProperty, "Employee4.FullName");
74+
75+
CheckBox checkBox4 = new CheckBox { HorizontalOptions = LayoutOptions.End };
76+
checkBox4.SetBinding(CheckBox.IsCheckedProperty, new MultiBinding
77+
{
78+
Bindings = new Collection<BindingBase>
79+
{
80+
new Binding("Employee4.IsOver16"),
81+
new Binding("Employee4.HasPassedTest"),
82+
new Binding("Employee4.IsSuspended", converter: new InverterConverter())
83+
},
84+
Converter = new AllTrueMultiConverter()
85+
});
86+
87+
Label employee5 = new Label { VerticalTextAlignment = TextAlignment.Center };
88+
employee5.SetBinding(Label.TextProperty, "Employee5.FullName");
89+
90+
CheckBox checkBox5 = new CheckBox { HorizontalOptions = LayoutOptions.End };
91+
checkBox5.SetBinding(CheckBox.IsCheckedProperty, new MultiBinding
92+
{
93+
Bindings = new Collection<BindingBase>
94+
{
95+
new Binding("Employee5.IsOver16"),
96+
new Binding("Employee5.HasPassedTest"),
97+
new Binding("Employee5.IsSuspended", converter: new InverterConverter())
98+
},
99+
Converter = new AllTrueMultiConverter()
100+
});
101+
102+
grid.Children.Add(employee1, 0, 1);
103+
grid.Children.Add(checkBox1, 1, 1);
104+
grid.Children.Add(employee2, 0, 2);
105+
grid.Children.Add(checkBox2, 1, 2);
106+
grid.Children.Add(employee3, 0, 3);
107+
grid.Children.Add(checkBox3, 1, 3);
108+
grid.Children.Add(employee4, 0, 4);
109+
grid.Children.Add(checkBox4, 1, 4);
110+
grid.Children.Add(employee5, 0, 5);
111+
grid.Children.Add(checkBox5, 1, 5);
112+
113+
Title = "MultiBinding converter demo";
114+
Content = grid;
115+
}
116+
}
117+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
using System.Collections.ObjectModel;
2+
using Xamarin.Forms;
3+
4+
namespace DataBindingDemos
5+
{
6+
public class MultiBindingStringFormatCodePage : ContentPage
7+
{
8+
public MultiBindingStringFormatCodePage()
9+
{
10+
BindingContext = new GroupViewModel();
11+
12+
Grid grid = new Grid { Margin = new Thickness(20) };
13+
14+
grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Auto) });
15+
grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Auto) });
16+
grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Auto) });
17+
grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Auto) });
18+
grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Auto) });
19+
grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Auto) });
20+
21+
Label employee1 = new Label();
22+
employee1.SetBinding(Label.TextProperty, new MultiBinding
23+
{
24+
Bindings = new Collection<BindingBase>
25+
{
26+
new Binding("Employee1.Forename"),
27+
new Binding("Employee1.MiddleName"),
28+
new Binding("Employee1.Surname")
29+
},
30+
StringFormat = "{0} {1} {2}"
31+
});
32+
33+
Label employee2 = new Label();
34+
employee2.SetBinding(Label.TextProperty, new MultiBinding
35+
{
36+
Bindings = new Collection<BindingBase>
37+
{
38+
new Binding("Employee2.Forename"),
39+
new Binding("Employee2.MiddleName"),
40+
new Binding("Employee2.Surname")
41+
},
42+
StringFormat = "{0} {1} {2}"
43+
});
44+
45+
Label employee3 = new Label();
46+
employee3.SetBinding(Label.TextProperty, new MultiBinding
47+
{
48+
Bindings = new Collection<BindingBase>
49+
{
50+
new Binding("Employee3.Forename"),
51+
new Binding("Employee3.MiddleName"),
52+
new Binding("Employee3.Surname")
53+
},
54+
StringFormat = "{0} {1} {2}"
55+
});
56+
57+
Label employee4 = new Label();
58+
employee4.SetBinding(Label.TextProperty, new MultiBinding
59+
{
60+
Bindings = new Collection<BindingBase>
61+
{
62+
new Binding("Employee4.Forename"),
63+
new Binding("Employee4.MiddleName"),
64+
new Binding("Employee4.Surname")
65+
},
66+
StringFormat = "{0} {1} {2}"
67+
});
68+
69+
Label employee5 = new Label();
70+
employee5.SetBinding(Label.TextProperty, new MultiBinding
71+
{
72+
Bindings = new Collection<BindingBase>
73+
{
74+
new Binding("Employee5.Forename"),
75+
new Binding("Employee5.MiddleName"),
76+
new Binding("Employee5.Surname")
77+
},
78+
StringFormat = "{0} {1} {2}"
79+
});
80+
81+
grid.Children.Add(new Label { Text = "Employee", FontAttributes = FontAttributes.Bold });
82+
grid.Children.Add(employee1, 0, 1);
83+
grid.Children.Add(employee2, 0, 2);
84+
grid.Children.Add(employee3, 0, 3);
85+
grid.Children.Add(employee4, 0, 4);
86+
grid.Children.Add(employee5, 0, 5);
87+
88+
Title = "MultiBinding code demo";
89+
Content = grid;
90+
}
91+
}
92+
}

0 commit comments

Comments
 (0)