在 Silverlight 中实现 IValueConverter 接口

发布于 2024-12-17 10:06:49 字数 2965 浏览 4 评论 0原文

我有以下 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 视为组合框中的项目。 ComboBox

可能是什么问题?

i have the following IValueConverter implementation

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
}

I have the following

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

The XAML is as follows

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

I just see the valueconverter.Product as the items in my combobox.
ComboBox

What could be the problem ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

狂之美人 2024-12-24 10:06:49

您需要指定 DataTemplate 是 ComboBox 的 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>

You need to specify that the DataTemplate is the ItemTemplate for the 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>
我家小可爱 2024-12-24 10:06:49

我像这样返回

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

并更改了以下内容

ImageUrl = @"Apple.jpg"

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

i made the return like this

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

and changed the following

ImageUrl = @"Apple.jpg"

Initially the BitMapImage was not getting populated with the image , it was showing null . My guess is this had something to do with me supplying bad URI ...So i got it to work !!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文