提供Image控件的源代码

时间:2012-02-03 17:29:52

标签: c# wpf xaml

我需要将数据绑定到自定义类中的元素中。我已经通过附加属性将ItemSource作为telerik的ObservableCollection:RadTransitionControl。但是,我需要提供图像成员作为Image控件的源。我尝试了以下方法,但没有成功。

<Grid Background="Black">
        <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=ImagePaths.AdImage}" />
                </DataTemplate>
            </telerik:RadTransitionControl.ContentTemplate>

        </telerik:RadTransitionControl>
    </Grid>

1 个答案:

答案 0 :(得分:1)

ImagePaths对象已设置为该项的DataContext。这意味着绑定已经指向(可以这么说)对象的一个​​实例。所以,当你告诉它绑定ImagePaths.AdImage时,它不知道如何找到你正在寻找的属性。好消息是,你所要做的就是提供对象的路径 - 删除ImagePaths部分(和点),你应该好好去。

例如......

class something 
{
  public string someImage {...}
}

<DataTemplate> <!--------- the data context of this item is an instance of
                           my "something" class, so i need to set the path
                           to be the property on the object --->

   <Image Source="{Binding Path=someImage}" />

</DataTemplate>

here is a very helpful article on debugging bindings in WPF
了解更多信息here is an excellent article on MSDN
here is a datatemplate article from Dr. WPF