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

怎甘沉沦 提交于 2019-12-01 08:16:08

问题


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 create a converter "string to string" for a gridview binding. This is for an imagesource. I take from an object a string( which is the name of the image) and I would like to add the "full path" like :

 return "ms-appdata:///local/" + value;

This is actually what I did :

 class thumbToFullPathConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        var fullPath = value;

        return ("ms-appdata:///local/" + value);
        Debug.WriteLine(value.ToString());
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        throw new NotImplementedException();
    }
}

Sorry, I think its a quick win but I don't know how to do this. Thanks for your time, Regards.


回答1:


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}" />



回答2:


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>


来源:https://stackoverflow.com/questions/15902304/winrt-c-sharp-create-converter-string-to-string-for-binding-gridview

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