【问题标题】:Value Converter doesn't work in CustomControl值转换器在 CustomControl 中不起作用
【发布时间】:2016-03-28 16:04:21
【问题描述】:

我尝试使用来自this post 的控件 - 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();
}

但是在这个用户控件中,在调试中,我从来没有进入这个转换器(当我在一个简单的单独图像上尝试它时,它可以工作)。

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


我用这个控件和转换器创建了一个测试项目 - 它可以工作......奇怪,非常奇怪


UPD:现在看起来像

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

根本不起作用。我试图设置另一个模板,但它也不起作用。 关于绑定 - 我添加了转换器只是为了查看绑定的内容,例如 here 并且绑定是正确的。


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

【问题讨论】:

  • 请检查您是否在资源块中使用“ImagePathConverter”名称声明了您的转换器。如果您只是使用类名,它将不起作用。
  • @VadimMartynov,是的,我做到了。
  • 不错。接下来确保您的绑定不会引发错误。您可以检查 Visual Studio 中的输出窗口以显示绑定错误。然后,确保您的属性绑定到此绑定。
  • @VadimMartynov,没有错误...让我再检查一下绑定
  • 请注意,转换器中的return bi.Clone() 没有意义。而是写return bi

标签: c# wpf xaml user-controls converter


【解决方案1】:

如何将属性 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>

不使用转换器?

【讨论】:

    猜你喜欢
    • 2017-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-24
    • 2021-04-28
    • 1970-01-01
    相关资源
    最近更新 更多