WinRT C# - create Converter string to string for binding Gridview

天大地大妈咪最大 提交于 2019-12-01 11:08:01

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>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!