Value Converter没有';不在CustomControl中工作

本文关键字:不在 CustomControl 工作 Converter 没有 Value | 更新日期: 2023-09-27 18:27:52

我尝试使用这篇文章中的控件-FlipView控件。这是我所拥有的:

<flipViewControl:FlipView ItemsSource="{Binding Images}" Name="ImagesFlipView"
                                            SelectedIndex="{Binding ElementName=ProductImagesBullets, Path=SelectedIndex, Mode=TwoWay}" Grid.Row="0">
    <flipViewControl:FlipView.ItemTemplate>
        <DataTemplate>
            <Image Source="{Binding FileLocation, Converter={StaticResource ImagePathConverter}}" Stretch="Fill"/>
        </DataTemplate>
    </flipViewControl:FlipView.ItemTemplate>
</flipViewControl:FlipView>

ImagePathConverter集图片来源:

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
    var url = string.Format("{0}{1}", ConfigurationManager.AppSettings["SiteUri"], value);
    var bi = new BitmapImage();
    bi.BeginInit();
    bi.CreateOptions = BitmapCreateOptions.IgnoreColorProfile;
    bi.CacheOption = BitmapCacheOption.OnDemand;
    bi.UriSource = new Uri(url);
    bi.EndInit();
    return bi.Clone();
}

但在这个用户控件中,在Debug中,我从未进入过这个转换器(当我在一个简单的单独映像上尝试它时,它是有效的)。

这个问题的解决办法是什么?(图像不显示,因为转换器没有为图像应用正确的ImageSource)


我用这个控制和转换器创建了一个测试项目,它工作。。。奇怪,非常奇怪的


UPD:现在看起来像

<flipViewControl:FlipView.ItemTemplate>
                                                <DataTemplate>
                                                    <TextBlock Text="{Binding FileLocation}"></TextBlock>
                                                    <!--<Image Source="{Binding FileLocation, Converter={StaticResource ImagePathConverter}}" Stretch="Fill"/>-->
                                                </DataTemplate>
                                            </flipViewControl:FlipView.ItemTemplate>

根本不起作用。我试图设置另一个模板,但它也不起作用。关于绑定-我添加了转换器,只是想看看这里的绑定是什么样的,绑定是正确的。


UPD:当我理解自定义控件应该作为单独的dll时,问题就解决了。所以我只是把我的代码移到单独的项目中,并添加对Main项目的引用。。。

Value Converter没有';不在CustomControl中工作

将属性FileLocation更改为类似的属性怎么样

public object FileLocation
{
    get
    {
        try
        {
             return new BitmapImage(new Uri((string)PathToImage));
        }
        catch 
        {
             return new BitmapImage();
        }
    }
}

XAML

<flipViewControl:FlipView ItemsSource="{Binding Images}" Name="ImagesFlipView" SelectedIndex="{Binding ElementName=ProductImagesBullets, Path=SelectedIndex, Mode=TwoWay}" Grid.Row="0">
    <flipViewControl:FlipView.ItemTemplate>
        <DataTemplate>
            <Image Source="{Binding FileLocation}" />  
        </DataTemplate>
    </flipViewControl:FlipView.ItemTemplate>
</flipViewControl:FlipView>

不使用转换器?