绑定到自定义类中的元素

时间:2012-02-03 08:58:15

标签: c# wpf xaml telerik

我有一个Telerik转换控件绑定到自定义类,转换控件接受需要一个图像作为输入。你能告诉我如何明确地告诉具有图像引用的元素名称。 Itemsource是附属物。我已经包含了以下代码。我尝试过Path = AdImage,但它没有用。

编辑:最近的不成功尝试,添加了DataTemplate:

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





<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 ImagePaths, Mode=OneWay,Path=AdImage}"
                                  VerticalAlignment="Center" 
                                  HorizontalAlignment="Center" Width="650">
        <telerik:RadTransitionControl.Transition>
            <telerik:MotionBlurredZoomTransition />
        </telerik:RadTransitionControl.Transition>

    </telerik:RadTransitionControl>
  

附加属性的代码如下

public static readonly DependencyProperty ItemsSourceProperty =
            DependencyProperty.RegisterAttached("ItemsSource", typeof(IEnumerable), typeof(AdRotatorExtensions), new PropertyMetadata(null, OnItemsSourceChanged));

private static void OnItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var element = d as FrameworkElement;
            var itemsControl = d as ItemsControl; // this is always null it returns a RadTranisitionControl object which doesnt have DisplayMemberName property

            var oldValue = e.OldValue as IEnumerable;
            var newValue = e.NewValue as IEnumerable;

                if (element != null)
                {
                    if (oldValue != null)
                    {
                        // Detach the Ad Rotator functionality.
                        element.Loaded -= OnElementLoaded;
                        element.Unloaded -= OnElementUnloaded;

                        // If there is a timer attached, stop it.
                        var timer = GetTimer(element);
                        if (timer != null)
                        {
                            timer.Stop();
                        }
                    }

                    if (newValue != null)
                    {
                        // Attach the Ad Rotator functionality.
                        element.Loaded += OnElementLoaded;
                        element.Unloaded += OnElementUnloaded;

                        // If the target is an ItemsControl and its ItemsSource is not set, set it.
                        if (itemsControl != null && itemsControl.ItemsSource == null && itemsControl.Items.Count == 0)
                        {
                            itemsControl.ItemsSource = newValue;
                            itemsControl.DisplayMemberPath = "AdImage"; // will never reaches here
                        }
                    }
                }
            }


    private static void OnElementLoaded(object sender, RoutedEventArgs args)
        {
            var element = sender as DependencyObject;

            // Create the timer and hook-up to the events.
            var timer = new DispatcherTimer();
            timer.Interval = GetItemChangeDelay(element).TimeSpan;
            SetTimer(element, timer);

            timer.Tick += (s, e) => MoveToNextElement(element);

            timer.Start();

            // Make sure the currently pointed element is selected.
            UpdateCurrentlySelectedItem(element);
        }

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

            ICommand updateValue = null;
            if (contentControl != null)
            {
                updateValue = contentControl.GetValue(IndexChangedProperty) as ICommand;
            }

            if (updateValue != null)
                if (updateValue.CanExecute(elementToSelect))
                {
                    updateValue.Execute(elementToSelect);


                }


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

            }
        }

0 个答案:

没有答案