使用ContentTemplate绑定时图像不显示

时间:2012-02-17 14:32:46

标签: c# wpf xaml

我有一个控件,我需要绑定以下对象的实例。但是图像未显示在控件中。请告诉我我做错了什么。

   public class DataNeeded
    {
        public string Name { get; set; }
        public Image AdImage { get; set; }




    }


 private  AdsCollection strs = new AdsCollection();

    public AdsCollection ImagePaths
    {
        get
        {
            DataNeeded dataNeed=new DataNeeded();
            dataNeed.Name = "Banner1";
            Image adimage = new Image();
            adimage.Source = new BitmapImage(new Uri(@"G:\adimages\pngs\banner1.png"));
            dataNeed.AdImage = adimage;
            strs.Add(dataNeed);

            DataNeeded dataNeed2 = new DataNeeded();
            dataNeed2.Name = "Banner2";
            Image adimage2 = new Image();
            adimage2.Source = new BitmapImage(new Uri(@"G:\adimages\pngs\banner2.png"));
            dataNeed2.AdImage = adimage2;
            strs.Add(dataNeed2);

            DataNeeded dataNeed3 = new DataNeeded();
            dataNeed3.Name = "Banner3";
            Image adimage3 = new Image();
            adimage3.Source = new BitmapImage(new Uri(@"G:\adimages\pngs\banner2.png"));
            dataNeed3.AdImage = adimage3;
            strs.Add(dataNeed3);

            //strs.Add(new BitmapImage(new Uri(@"G:\adimages\pngs\banner2.png")));
            //strs.Add(new BitmapImage(new Uri(@"G:\adimages\pngs\banner3.png")));
            //strs.Add(new BitmapImage(new Uri(@"G:\adimages\pngs\banner4.png")));

            return strs;

        }

控件的xaml如下

<telerik:RadTransitionControl   x:Name="radControl" adRotator:AdRotatorExtensions.ItemChangeDelay="0:0:3" 
                                  adRotator:AdRotatorExtensions.CurrentSelectedIndex="0"
                              adRotator:AdRotatorExtensions.IndexChanged="{Binding TopItemCommand, Mode=OneWay}"
                              adRotator:AdRotatorExtensions.ItemsSource="{Binding Path=ImagePaths}"
                                  VerticalAlignment="Center" 
                                  HorizontalAlignment="Center" Width="650">
        <telerik:RadTransitionControl.Transition>
            <telerik:MotionBlurredZoomTransition />
        </telerik:RadTransitionControl.Transition>

    <telerik:RadTransitionControl.ContentTemplate>
        <DataTemplate>
            <Image Source="{Binding Path=AdImage}" />
        </DataTemplate>
    </telerik:RadTransitionControl.ContentTemplate>

</telerik:RadTransitionControl>

1 个答案:

答案 0 :(得分:0)

从您的代码中我可以看到您在ViewModel对象中有Image控件,并且您将ContentTemplate中的Image控件绑定到数据对象的Image控件,这是不正确的。您可以使用ContentPresenter(它可以显示图像控件)替换contentTemplate中的Image控件,从数据对象绑定Image的Source属性中的Image控件,或者完全更改数据对象。

在我看来,最好的解决方案是按如下方式更改DataNeeded类:

public class DataNeeded
{
    public string Name { get; set; }
    public ImageSource AdImage { get; set; }
}

希望这有帮助。