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

后端 未结 2 1296
旧巷少年郎
旧巷少年郎 2021-01-15 05:29

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

相关标签:
2条回答
  • 2021-01-15 06:24

    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}" />
    
    0 讨论(0)
  • 2021-01-15 06:29

    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>
    
    0 讨论(0)
提交回复
热议问题