Skip to content

Instantly share code, notes, and snippets.

@ytabuchi
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ytabuchi/467fc7edd4bb51acb204 to your computer and use it in GitHub Desktop.
Save ytabuchi/467fc7edd4bb51acb204 to your computer and use it in GitHub Desktop.
Xamarin.Forms で IValueConverter を使う ref: http://qiita.com/ytabuchi/items/623eca74c4377d8afba1
<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>
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
}
};
<ContentPage.Resources>
<ResourceDictionary>
<local:StringToUpperConverter x:Key="s2uConverter" />
<local:StringToIntConverter x:Key="s2iConverter" />
</ResourceDictionary>
</ContentPage.Resources>
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>
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;
<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>
string _message;
public string Message
{
get { return _message; }
set
{
if (_message != value)
{
_message = value;
OnPropertyChanged("Message");
}
}
}
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
}
};
if (value == null)
return 0;
<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>
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