1

I am using an telerik radanimationcontrol along with some third party attached properties to create an adRotator in wpf. the third party attached property have a documentation saying that the current index of the ad showing is returned in SelectedIndexProperty. However,I am trying to access the SelectedIndexProperty using the following method.

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

The above statement results in an InvalidCastException. Can any one please tell me how to access the property via code behind. my xaml markup for the radAnimationControl is as following

<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>

Also, please tell is it possible to set the itemsource to a observablecollection which is a collection of a custom class and get the custom class reference instead of SelectedIndex as int.

I tried this link but didn't help me.

the other thing i tried is as follows

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

The GetCurrentSelectedIndex is defined as

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))

Stack Trace for exception happend while calling 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, 

Stack Trace for exception happend while calling 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()
Community
  • 1
  • 1
logeeks
  • 4,849
  • 15
  • 62
  • 93
  • Is the type of the `CurrentSelectedIndex` property really a `long`? Are you sure it's not `int`? Otherwise, you should be including the message in the exception so we could be of more help. – Jeff Mercado Jan 30 '12 at 07:27
  • updated the question. please see if it is of any help. – logeeks Jan 30 '12 at 07:41
  • The exception is the key here. Post what the message was and the stack trace. There's no way we can tell which line of code is throwing it. – Jeff Mercado Jan 30 '12 at 07:49
  • The type of the dependency property is actually int, not long, so replace `long si = (long)TranControl.GetValue(...)` by `int si = (int)TranControl.GetValue(...)` – Clemens Jan 30 '12 at 09:01

0 Answers0