【问题标题】:Implementing IValueConverter interface in Silverlight在 Silverlight 中实现 IValueConverter 接口
【发布时间】:2011-11-21 17:54:54
【问题描述】:

我有以下 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>

我只是将 valueconverter.Product 视为组合框中的项目。

可能是什么问题?

【问题讨论】:

    标签: c# silverlight silverlight-4.0 ivalueconverter


    【解决方案1】:

    您需要指定DataTemplate 是组合框的ItemTemplate

      <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>
    

    【讨论】:

    • 这显示了产品的名称,图像仍然没有显示...
    【解决方案2】:

    我是这样退货的

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

    并更改了以下内容

    ImageUrl = @"Apple.jpg"
    

    最初 BitMapImage 没有填充图像,它显示为 null 。我的猜测是这与我提供了错误的 URI 有关......所以我让它工作了!!

    【讨论】:

    • 是的,您正在尝试从本地文件系统获取图像。 Silverlight 不会让你这样做。
    猜你喜欢
    • 2012-04-06
    • 1970-01-01
    • 1970-01-01
    • 2022-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多