Реализация интерфейса IValueConverter в Silverlight - PullRequest
2 голосов
/ 21 ноября 2011

У меня есть следующая реализация IValueConverter

public class MyValueConverter : IValueConverter
{
    #region IValueConverter Members

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        try
        {
            var uri = new Uri((string)(value), UriKind.RelativeOrAbsolute);
            var img = new BitmapImage(uri);
            return img;
        }
        catch
        {
            return new BitmapImage();
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {

        var img = value as BitmapImage;
        return img.UriSource.AbsoluteUri;
    }

    #endregion
}

У меня есть следующее

public partial class MainPage : UserControl
{
    public MainPage()
    {
        InitializeComponent();
        var products = new List<Product>()
                     {
                         new Product()
                             {
                                 Name = "Apple",
                                 ImageUrl = @"c:\users\ashutosh\documents\visual studio 2010\Projects\ValueConverter\ValueConverter\Images\Apple.jpg"
                             },
                         new Product()
                             {
                                 Name = "Mango",
                                 ImageUrl = @"c:\users\ashutosh\documents\visual studio 2010\Projects\ValueConverter\ValueConverter\Images\Mango.jpg"
                             }
                     };

        myComboBox.Items.Clear();   
        myComboBox.ItemsSource = products;
    }
}

XAML выглядит следующим образом

<UserControl x:Class="ValueConverter.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    xmlns:this="clr-namespace:ValueConverter"
    d:DesignHeight="150" d:DesignWidth="200">
<UserControl.Resources>
        <this:MyValueConverter x:Key="ImageConverter"/> 
</UserControl.Resources>
    <Grid x:Name="LayoutRoot" Background="White">
        <ComboBox Name="myComboBox"  Height="143" Width="193">
            <DataTemplate>
                <StackPanel Orientation="Vertical">
                    <TextBox Text="{Binding Name}"></TextBox>
                    <Image Source="{Binding ImageUrl, Converter={StaticResource ImageConverter},Mode=TwoWay}"></Image>
                </StackPanel>
            </DataTemplate>
        </ComboBox>
    </Grid>
</UserControl>

Я просто вижу значение конвертера. ComboBox

В чем может быть проблема?

Ответы [ 2 ]

3 голосов
/ 21 ноября 2011

Необходимо указать, что DataTemplate является ItemTemplate для ComboBox:

  <ComboBox Name="myComboBox"  Height="143" Width="193">
     <!-- Add this! -->
     <ComboBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Vertical">
                <TextBox Text="{Binding Name}"></TextBox>
                <Image Source="{Binding ImageUrl, Converter={StaticResource ImageConverter},Mode=TwoWay}"></Image>
            </StackPanel>
        </DataTemplate>
    </ComboBox.ItemTemplate>
  </ComboBox>
0 голосов
/ 21 ноября 2011

я сделал возврат вот так

return new BitmapImage(new Uri("../Images/" + (string)(value), UriKind.Relative));

и изменил следующее

ImageUrl = @"Apple.jpg"

Первоначально BitMapImage не заполнялся изображением, он показывал ноль. Я предполагаю, что это как-то связано со мной, предоставляющим плохой URI ... Так что я заставил его работать !!

...