Last active
August 29, 2015 14:23
-
-
Save ytabuchi/467fc7edd4bb51acb204 to your computer and use it in GitHub Desktop.
Xamarin.Forms で IValueConverter を使う ref: http://qiita.com/ytabuchi/items/623eca74c4377d8afba1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<ContentPage.Resources> | |
<ResourceDictionary> | |
<cv:StringCaseConverter x:Key="scConverter" /> | |
<cv:StringToLengthConverter x:Key="s2lConverter" /> | |
</ResourceDictionary> | |
</ContentPage.Resources> | |
<StackLayout> | |
<Editor x:Name="editor" Text="test" /> | |
<!-- ConverterParameter は "True" で UpperCase、"False" で LowerCase になります。 | |
今は固定してしまっていますが、Switch とかから IsToggled を参照したいですね。 --> | |
<Label BindingContext="{x:Reference Name=editor}" | |
Text="{Binding Text, | |
Converter={StaticResource scConverter}, | |
ConverterParameter=True}" /> | |
<Label BindingContext="{x:Reference Name=editor}" | |
Text="{Binding Text, | |
Converter={StaticResource s2lConverter}, | |
StringFormat='{0} letters'}" /> | |
</StackLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var editor = new Editor { Text = "test" }; | |
var sclabel = new Label { Text = "" }; | |
sclabel.BindingContext = editor; | |
sclabel.SetBinding(Label.TextProperty, | |
new Binding("Text", | |
converter: new Converters.StringCaseConverter(), | |
converterParameter: "True")); | |
var sllabel = new Label { Text = "" }; | |
sllabel.BindingContext = editor; | |
sllabel.SetBinding(Label.TextProperty, | |
new Binding("Text", | |
converter: new Converters.StringToLengthConverter(), | |
stringFormat: "{0} letters")); | |
Content = new StackLayout | |
{ | |
Children = { | |
editor, | |
sclabel, | |
sllabel | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<ContentPage.Resources> | |
<ResourceDictionary> | |
<local:StringToUpperConverter x:Key="s2uConverter" /> | |
<local:StringToIntConverter x:Key="s2iConverter" /> | |
</ResourceDictionary> | |
</ContentPage.Resources> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
xmlns:cv="clr-namespace:XF_IValueConverterSample.Converters;assembly=XF_IValueConverterSample" | |
xmlns:vm="clr-namespace:XF_IValueConverterSample.ViewModel;assembly=XF_IValueConverterSample" | |
<ContentPage.BindingContext> | |
<vm:CommonViewModel /> | |
</ContentPage.BindingContext> | |
<ContentPage.Resources> | |
<ResourceDictionary> | |
<cv:StringCaseConverter x:Key="scConverter" /> | |
<cv:StringToLengthConverter x:Key="s2lConverter" /> | |
</ResourceDictionary> | |
</ContentPage.Resources> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var listdata = new List<Words> { | |
new Words { word = "the" }, | |
new Words { word = "quick" }, | |
new Words { word = "brown" }, | |
new Words { word = "fox" }, | |
new Words { word = "jumps" }, | |
new Words { word = "over" }, | |
new Words { word = "the" }, | |
new Words { word = "lazy" }, | |
new Words { word = "dog" } | |
}; | |
this.BindingContext = listdata; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<StackLayout> | |
<Editor Text="{Binding Message}" /> | |
<Label Text="{Binding Message}" /> | |
<Label Text="{Binding Message, | |
Converter={StaticResource scConverter}, | |
ConverterParameter=True}" /> | |
<Label Text="{Binding Message, | |
Converter={StaticResource s2lConverter}, | |
StringFormat='{0} letters'}" /> | |
</StackLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
string _message; | |
public string Message | |
{ | |
get { return _message; } | |
set | |
{ | |
if (_message != value) | |
{ | |
_message = value; | |
OnPropertyChanged("Message"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
BindingContext = new ViewModel.CommonViewModel(); | |
var editor = new Editor { Text = "" }; | |
editor.SetBinding(Editor.TextProperty, "Message"); | |
var sclabel = new Label { Text = "" }; | |
sclabel.SetBinding(Label.TextProperty, | |
new Binding("Message", | |
converter: new Converters.StringCaseConverter(), | |
converterParameter: "True")); | |
var sllabel = new Label { Text = "" }; | |
sllabel.SetBinding(Label.TextProperty, | |
new Binding("Message", | |
converter: new Converters.StringToLengthConverter(), | |
stringFormat: "{0} letters")); | |
Content = new StackLayout | |
{ | |
Children = { | |
editor, | |
sclabel, | |
sllabel | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (value == null) | |
return 0; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<ListView ItemsSource="{Binding}"> | |
<ListView.ItemTemplate> | |
<DataTemplate> | |
<TextCell Text="{Binding word, | |
Converter={StaticResource scConverter}, | |
ConverterParameter=True}" | |
Detail="{Binding word, | |
Converter={StaticResource s2lConverter}, | |
StringFormat='{0} letters'}" /> | |
</DataTemplate> | |
</ListView.ItemTemplate> | |
</ListView> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var listdata = new List<Words> { | |
new Words { word = "the" }, | |
new Words { word = "quick" }, | |
new Words { word = "brown" }, | |
new Words { word = "fox" }, | |
new Words { word = "jumps" }, | |
new Words { word = "over" }, | |
new Words { word = "the" }, | |
new Words { word = "lazy" }, | |
new Words { word = "dog" } | |
}; | |
this.BindingContext = listdata; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment