I come to you today for a \"little\" problem. I don\'t know how to create a simple converter because its the first time and I don\'t find a easy example. I would like to cre
You want your class to inherit from the IValueConverter
interface.
public class ThumbToFullPathConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
if (value == null)
return value;
return String.Format("ms-appdata:///local/{0}", value.ToString());
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
You then need to include this converter in your XAML (either as a resource local to the page, or an app resource available throughout the app). Import the namespace where on the page(s) you want to access the converter. (Change MyConverters
to your namespace)
xmlns:local="clr-namespace:MyConverters"
Then set it as a resource
<MyPage.Resources>
<local:ThumbToFullPathConverter x:Key="ThumbToFullPathConverter" />
</MyPage.Resources>
Then you can use it where you like
<TextBlock Text="{Binding MyText, Converter={StaticResource ThumbToFullPathConverter}" />
Add a class with this code. It will be your converter
public class ThumbToFullPathConverter : IValueConverter
{
public object Convert(object value, Type targettype, object parameter, string Path)
{
return ("ms-appdata:///local/" + value).ToString();
}
public object ConvertBack(object value, Type targettype, object parameter, string Path)
{
throw new NotImplementedException();
}
}
Now the below code will explain you how can you use it to bind image in gridview data template.
Add the page resource in you XAMl page.
<Page.Resources>
<local:ThumbToFullPathConverter
x:Key="ThumbToFullPathConverter" />
</Page.Resources>
<DataTemplate x:Key="MyTemplate">
<Image
Source="{Binding path, Converter={StaticResource ThumbToFullPathConverter}}"
Stretch="None" />
</DataTemplate>