我正在使用telerik radanimationcontrol以及一些第三方附加属性来在wpf中创建adRotator . 第三方附加属性有一个文档说明广告显示的当前索引是在SelectedIndexProperty中返回的 . 但是,我试图使用以下方法访问SelectedIndexProperty .

long si =(long)TranControl.GetValue(AdRotatorExtensions.AdRotatorExtensions.CurrentSelectedIndexProperty) ;

上面的语句导致InvalidCastException . 任何人都可以告诉我如何通过代码后面访问该属性 . 我对radAnimationControl的xaml标记如下

<telerik:RadTransitionControl Name="TranControl"  adRotator:AdRotatorExtensions.ItemChangeDelay="0:0:3" 
                                      adRotator:AdRotatorExtensions.CurrentSelectedIndex="0"
                                      VerticalAlignment="Center" 
                                      HorizontalAlignment="Center" MouseLeftButtonDown="RadTransitionControl_MouseLeftButtonDown">
            <telerik:RadTransitionControl.Transition>
                <telerik:MotionBlurredZoomTransition />
            </telerik:RadTransitionControl.Transition>
            <adRotator:AdRotatorExtensions.ItemsSource>
                <adRotator:AdsCollection >
                    <Image Source="/Banners/1_AJAX_Slide.jpg" />
                    <Image Source="/Banners/2_MVC_Slide.jpg" />
                    <Image Source="/Banners/3_SL_Slide.jpg" />
                    <Image Source="/Banners/4_WF_Slide.jpg" />
                    <Image Source="/Banners/5_WPF_Slide.jpg" />
                    <Image Source="/Banners/6_REP_Slide.jpg" />
                    <Image Source="/Banners/7_ORM_Slide.jpg" />
                    <Image Source="/Banners/8_JC_Slide.jpg" />
                    <Image Source="/Banners/9_JM_Slide.jpg" />
                </adRotator:AdsCollection>
            </adRotator:AdRotatorExtensions.ItemsSource>
        </telerik:RadTransitionControl>

另外,请告诉我是否可以将itemsource设置为observablecollection,它是自定义类的集合,并获取自定义类引用而不是SelectedIndex作为int .

我试过this link但没有帮助我 .

我试过的另一件事情如下

int currentSelectedIndex = AdRotatorExtensions.AdRotatorExtensions.GetCurrentSelectedIndex(null);

GetCurrentSelectedIndex定义为

public static readonly DependencyProperty CurrentSelectedIndexProperty =
            DependencyProperty.RegisterAttached("CurrentSelectedIndex", typeof(int), typeof(AdRotatorExtensions), new PropertyMetadata(-1, OnCurrentSelectedIndexChanged));

private static void OnCurrentSelectedIndexChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            UpdateCurrentlySelectedItem(d);
        }


    private static void UpdateCurrentlySelectedItem(DependencyObject element)
    {
        var contentControl = element as ContentControl;

        var source = GetItemsSource(element);

        // If there is no source we shouldn't do anything.
        if (source == null) return;

        // Find the actual index to be selected (if outside the boundaries of the collection)
        // and find the actual element to be selected.
        var convertedSource = source.Cast<object>();
        var currentIndex = GetCurrentSelectedIndex(element);
        var elementToSelect = convertedSource.ElementAtOrDefault(currentIndex);

        // Update the cotnent of the ContentControl if attached to a ContentControl.
        if (contentControl != null)
        {
            contentControl.Content = elementToSelect;
        }
    }

public static int GetCurrentSelectedIndex(DependencyObject obj)
        {
            return (int)obj.GetValue(CurrentSelectedIndexProperty);


    }

public static readonly DependencyProperty CurrentSelectedIndexProperty = DependencyProperty.RegisterAttached(“CurrentSelectedIndex”,typeof(int),typeof(AdRotatorExtensions),new PropertyMetadata(-1,OnCurrentSelectedIndexChanged))

在调用int currentSelectedIndex = AdRotatorExtensions.AdRotatorExtensions.GetCurrentSelectedIndex(null);时,堆栈跟踪异常发生

at AdRotatorExtensions.AdRotatorExtensions.GetCurrentSelectedIndex(DependencyObject obj) in G:\Raj\Kiosk\AdRotatorExample Demo project\AdRotatorExtensions\AdRotatorExtensions.cs:line 47
   at AdRotatorExampleWPF.MainWindow.RadTransitionControl_MouseLeftButtonDown(Object sender, MouseButtonEventArgs e) in G:\Raj\Kiosk\AdRotatorExample Demo project\AdRotatorExampleWPF\MainWindow.xaml.cs:line 19
   at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
   at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
   at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
   at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
   at System.Windows.UIElement.ReRaiseEventAs(DependencyObject sender, RoutedEventArgs args, RoutedEvent newEvent)
   at System.Windows.UIElement.CrackMouseButtonEventAndReRaiseEvent(DependencyObject sender,

调用异常发生的堆栈跟踪调用long si =(long)TranControl.GetValue(AdRotatorExtensions.AdRotatorExtensions.CurrentSelectedIndexProperty);:

at AdRotatorExampleWPF.MainWindow.RadTransitionControl_MouseLeftButtonDown(Object sender, MouseButtonEventArgs e) in G:\Raj\Kiosk\AdRotatorExample Demo project\AdRotatorExampleWPF\MainWindow.xaml.cs:line 20
   at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
   at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
   at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
   at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
   at System.Windows.UIElement.ReRaiseEventAs(DependencyObject sender, RoutedEventArgs args, RoutedEvent newEvent)
   at System.Windows.UIElement.CrackMouseButtonEventAndReRaiseEvent(DependencyObject sender, MouseButtonEventArgs e)
   at System.Windows.UIElement.OnMouseDownThunk(Object sender, MouseButtonEventArgs e)
   at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
   at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
   at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
   at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
   at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
   at System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted)
   at System.Windows.Input.InputManager.ProcessStagingArea()
   at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)
   at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)
   at System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel)
   at System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Boolean isSingleParameter)
   at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
   at System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
   at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Boolean isSingleParameter)
   at System.Windows.Threading.Dispatcher.Invoke(DispatcherPriority priority, Delegate method, Object arg)
   at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
   at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
   at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
   at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
   at System.Windows.Threading.Dispatcher.Run()
   at System.Windows.Application.RunDispatcher(Object ignore)
   at System.Windows.Application.RunInternal(Window window)
   at System.Windows.Application.Run(Window window)
   at System.Windows.Application.Run()
   at AdRotatorExampleWPF.App.Main() in G:\Raj\Kiosk\AdRotatorExample Demo project\AdRotatorExampleWPF\obj\x86\Debug\App.g.cs:line 0
   at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()