WinRT的C# - 创建转换器串来串绑定的GridView [英] WinRT C# - create Converter string to string for binding Gridview

查看:104
本文介绍了WinRT的C# - 创建转换器串来串绑定的GridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我今天要你来一个小的问题。我不知道如何创建一个简单的转换,因为它的第一次,我没有找到一个简单的例子。
我想创建一个转换器串来串的gridview的绑定。这是一个的ImageSource。我从一个对象的字符串(这是图像的名字),我想补充的完整路径,如:

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;

这其实是我做过什么:

 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.

推荐答案

您希望您的类从的IValueConverter 接口继承。

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();
    }
}

您再需要在您的XAML此转换器(无论是作为一个资源本地的页面或整个应用程序提供一个应用程序的资源)。导入所在的网页上,你要访问的转换器的命名空间。 (更改 MyConverters 来命名空间)

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

这篇关于WinRT的C# - 创建转换器串来串绑定的GridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆